-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "password" TEXT,
    "salt" TEXT,
    "phone" TEXT,
    "email" TEXT NOT NULL,
    "avatar" TEXT,
    "is_system" BOOLEAN,
    "is_admin" BOOLEAN,
    "last_sign_time" TIMESTAMP(3),
    "deactivated_time" TIMESTAMP(3),
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_time" TIMESTAMP(3),
    "last_modified_time" TIMESTAMP(3),

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "attachments" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "hash" TEXT NOT NULL,
    "size" INTEGER NOT NULL,
    "mimetype" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "width" INTEGER,
    "height" INTEGER,
    "deleted_time" TIMESTAMP(3),
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" TEXT NOT NULL,
    "last_modified_by" TEXT,
    "thumbnail_path" TEXT,

    CONSTRAINT "attachments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "notification" (
    "id" TEXT NOT NULL,
    "from_user_id" TEXT NOT NULL,
    "to_user_id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "url_path" TEXT,
    "is_read" BOOLEAN NOT NULL DEFAULT false,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" TEXT NOT NULL,

    CONSTRAINT "notification_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "setting" (
    "instance_id" TEXT NOT NULL,
    "disallow_sign_up" BOOLEAN,
    "disallow_space_creation" BOOLEAN,
    "disallow_space_invitation" BOOLEAN,
    "enable_email_verification" BOOLEAN,
    "ai_config" TEXT,
    "brand_name" TEXT,
    "brand_logo" TEXT,

    CONSTRAINT "setting_pkey" PRIMARY KEY ("instance_id")
);

-- CreateTable
CREATE TABLE "trash" (
    "id" TEXT NOT NULL,
    "resource_type" TEXT NOT NULL,
    "resource_id" TEXT NOT NULL,
    "parent_id" TEXT,
    "deleted_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_by" TEXT NOT NULL,

    CONSTRAINT "trash_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_last_visit" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "resource_type" TEXT NOT NULL,
    "resource_id" TEXT NOT NULL,
    "parent_resource_id" TEXT NOT NULL,
    "last_visit_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_last_visit_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_clients" (
    "id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "client_secret" TEXT,
    "client_name" TEXT NOT NULL,
    "client_uri" TEXT,
    "logo_uri" TEXT,
    "contacts" TEXT[],
    "redirect_uris" TEXT[],
    "post_logout_redirect_uris" TEXT[],
    "token_endpoint_auth_method" TEXT NOT NULL,
    "grant_types" TEXT[],
    "response_types" TEXT[],
    "scope" TEXT NOT NULL,
    "jwks_uri" TEXT,
    "jwks" TEXT,
    "policy_uri" TEXT,
    "tos_uri" TEXT,
    "require_pkce" BOOLEAN NOT NULL DEFAULT false,
    "active" BOOLEAN NOT NULL DEFAULT true,
    "created_by" TEXT,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_modified_time" TIMESTAMP(3),

    CONSTRAINT "oidc_clients_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_consents" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "scope" TEXT NOT NULL,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "oidc_consents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_authorization_codes" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "scope" TEXT NOT NULL,
    "redirect_uri" TEXT NOT NULL,
    "code_challenge" TEXT,
    "code_challenge_method" TEXT,
    "nonce" TEXT,
    "auth_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "used" BOOLEAN NOT NULL DEFAULT false,

    CONSTRAINT "oidc_authorization_codes_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_tokens" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "token_type" TEXT NOT NULL,
    "scope" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_revoked" BOOLEAN NOT NULL DEFAULT false,
    "parent_id" TEXT,

    CONSTRAINT "oidc_tokens_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_sessions" (
    "id" TEXT NOT NULL,
    "session_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "last_active" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "device_info" TEXT,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_modified_time" TIMESTAMP(3),

    CONSTRAINT "oidc_sessions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "oidc_key_pairs" (
    "id" TEXT NOT NULL,
    "kid" TEXT NOT NULL,
    "private_key" TEXT NOT NULL,
    "public_key" TEXT NOT NULL,
    "algorithm" TEXT NOT NULL,
    "active" BOOLEAN NOT NULL DEFAULT true,
    "created_time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "oidc_key_pairs_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "users_phone_key" ON "users"("phone");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "attachments_token_key" ON "attachments"("token");

-- CreateIndex
CREATE INDEX "notification_to_user_id_is_read_created_time_idx" ON "notification"("to_user_id", "is_read", "created_time");

-- CreateIndex
CREATE UNIQUE INDEX "trash_resource_type_resource_id_key" ON "trash"("resource_type", "resource_id");

-- CreateIndex
CREATE INDEX "user_last_visit_user_id_resource_type_idx" ON "user_last_visit"("user_id", "resource_type");

-- CreateIndex
CREATE UNIQUE INDEX "user_last_visit_user_id_resource_type_parent_resource_id_key" ON "user_last_visit"("user_id", "resource_type", "parent_resource_id");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_clients_client_id_key" ON "oidc_clients"("client_id");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_consents_user_id_client_id_key" ON "oidc_consents"("user_id", "client_id");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_authorization_codes_code_key" ON "oidc_authorization_codes"("code");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_tokens_token_key" ON "oidc_tokens"("token");

-- CreateIndex
CREATE INDEX "oidc_tokens_user_id_token_type_is_revoked_idx" ON "oidc_tokens"("user_id", "token_type", "is_revoked");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_sessions_session_id_key" ON "oidc_sessions"("session_id");

-- CreateIndex
CREATE UNIQUE INDEX "oidc_key_pairs_kid_key" ON "oidc_key_pairs"("kid");

-- AddForeignKey
ALTER TABLE "oidc_consents" ADD CONSTRAINT "oidc_consents_client_id_fkey" FOREIGN KEY ("client_id") REFERENCES "oidc_clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "oidc_authorization_codes" ADD CONSTRAINT "oidc_authorization_codes_client_id_fkey" FOREIGN KEY ("client_id") REFERENCES "oidc_clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "oidc_tokens" ADD CONSTRAINT "oidc_tokens_client_id_fkey" FOREIGN KEY ("client_id") REFERENCES "oidc_clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;
