-- 增量迁移：添加 WorkProcess、EnterpriseTemplate、AttendancePublish 三张表
-- 不影响现有数据，仅 CREATE TABLE IF NOT EXISTS
-- 执行方式: PGPASSWORD=turbohr2026 psql -U turbohr -d turbohr -f migration_add_three_tables.sql

-- 1. WorkProcess 用工办理工作流
CREATE TABLE IF NOT EXISTS "WorkProcess" (
    "id"          TEXT NOT NULL,
    "orgId"       TEXT NOT NULL,
    "type"        TEXT NOT NULL,
    "title"       TEXT NOT NULL,
    "employeeId"  TEXT,
    "status"      TEXT NOT NULL DEFAULT 'DRAFT',
    "formData"    JSONB NOT NULL,
    "documents"   JSONB,
    "approverId"  TEXT,
    "approvedAt"  TIMESTAMP(3),
    "remark"      TEXT,
    "createdBy"   TEXT NOT NULL,
    "createdAt"   TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt"   TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- 外键：orgId -> Organization
ALTER TABLE "WorkProcess"
    ADD CONSTRAINT "WorkProcess_orgId_fkey"
    FOREIGN KEY ("orgId") REFERENCES "Organization"("id") ON DELETE CASCADE;

-- 外键：employeeId -> Employee (ON DELETE SET NULL)
ALTER TABLE "WorkProcess"
    ADD CONSTRAINT "WorkProcess_employeeId_fkey"
    FOREIGN KEY ("employeeId") REFERENCES "Employee"("id") ON DELETE SET NULL;

-- 索引
CREATE INDEX IF NOT EXISTS "WorkProcess_orgId_status_idx" ON "WorkProcess"("orgId", "status");
CREATE INDEX IF NOT EXISTS "WorkProcess_orgId_type_idx" ON "WorkProcess"("orgId", "type");
CREATE INDEX IF NOT EXISTS "WorkProcess_orgId_createdBy_idx" ON "WorkProcess"("orgId", "createdBy");

-- 2. EnterpriseTemplate 企业自建文本模板
CREATE TABLE IF NOT EXISTS "EnterpriseTemplate" (
    "id"          TEXT NOT NULL,
    "orgId"       TEXT NOT NULL,
    "name"        TEXT NOT NULL,
    "category"    TEXT NOT NULL,
    "description" TEXT,
    "content"     TEXT NOT NULL,
    "variables"   TEXT[] NOT NULL DEFAULT '{}',
    "status"      TEXT NOT NULL DEFAULT 'ACTIVE',
    "createdBy"   TEXT NOT NULL,
    "createdAt"   TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt"   TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

ALTER TABLE "EnterpriseTemplate"
    ADD CONSTRAINT "EnterpriseTemplate_orgId_fkey"
    FOREIGN KEY ("orgId") REFERENCES "Organization"("id") ON DELETE CASCADE;

CREATE INDEX IF NOT EXISTS "EnterpriseTemplate_orgId_category_idx" ON "EnterpriseTemplate"("orgId", "category");

-- 3. AttendancePublish 考勤发布记录
CREATE TABLE IF NOT EXISTS "AttendancePublish" (
    "id"          TEXT NOT NULL,
    "orgId"       TEXT NOT NULL,
    "month"       TEXT NOT NULL,
    "title"       TEXT NOT NULL,
    "status"      TEXT NOT NULL DEFAULT 'PUBLISHED',
    "publishDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdBy"   TEXT NOT NULL,
    "createdAt"   TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

ALTER TABLE "AttendancePublish"
    ADD CONSTRAINT "AttendancePublish_orgId_fkey"
    FOREIGN KEY ("orgId") REFERENCES "Organization"("id") ON DELETE CASCADE;

-- 唯一约束：同一组织同月只能有一条发布记录
CREATE UNIQUE INDEX IF NOT EXISTS "AttendancePublish_orgId_month_key" ON "AttendancePublish"("orgId", "month");

CREATE INDEX IF NOT EXISTS "AttendancePublish_orgId_month_idx" ON "AttendancePublish"("orgId", "month");
