-- CreateEnum
CREATE TYPE "corp_approval"."ApprovalAdminExportStatus" AS ENUM ('PENDING', 'PROCESSING', 'SUCCESS', 'FAILED', 'EXPIRED');

-- CreateTable
CREATE TABLE "corp_approval"."approval_admin_configs" (
    "id" UUID NOT NULL,
    "key" TEXT NOT NULL,
    "value" JSONB NOT NULL,
    "description" TEXT,
    "updated_by" UUID,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "corp_approval"."approval_admin_exports" (
    "id" UUID NOT NULL,
    "status" "corp_approval"."ApprovalAdminExportStatus" NOT NULL DEFAULT 'PENDING',
    "format" VARCHAR(10) NOT NULL DEFAULT 'xlsx',
    "requested_by" UUID NOT NULL,
    "filters" JSONB NOT NULL DEFAULT '{}',
    "file_key" TEXT,
    "file_name" TEXT,
    "file_size" BIGINT,
    "content_type" TEXT,
    "total_rows" INTEGER,
    "error_message" TEXT,
    "started_at" TIMESTAMPTZ(3),
    "completed_at" TIMESTAMPTZ(3),
    "expires_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "approval_admin_configs_key_key" ON "corp_approval"."approval_admin_configs"("key");

-- CreateIndex
CREATE INDEX "approval_admin_exports_status_idx" ON "corp_approval"."approval_admin_exports"("status");

-- CreateIndex
CREATE INDEX "approval_admin_exports_requested_by_created_at_idx" ON "corp_approval"."approval_admin_exports"("requested_by", "created_at" DESC);

-- CreateIndex
CREATE INDEX "approval_admin_exports_expires_at_idx" ON "corp_approval"."approval_admin_exports"("expires_at");

-- AddForeignKey
ALTER TABLE "corp_approval"."approval_admin_exports" ADD CONSTRAINT "approval_admin_exports_requested_by_fkey" FOREIGN KEY ("requested_by") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
