-- 员工特殊状态台账表（三期/工伤/医疗期等）
CREATE TABLE IF NOT EXISTS "EmployeeSpecialStatus" (
    "id"              TEXT NOT NULL,
    "orgId"           TEXT NOT NULL,
    "employeeId"      TEXT NOT NULL,
    "type"            TEXT NOT NULL,
    "status"          TEXT NOT NULL DEFAULT 'ACTIVE',
    "startDate"       TIMESTAMP(3),
    "endDate"         TIMESTAMP(3),
    "actualEndDate"   TIMESTAMP(3),
    "expectedDueDate"      TIMESTAMP(3),
    "maternityLeaveStart"  TIMESTAMP(3),
    "maternityLeaveEnd"    TIMESTAMP(3),
    "nursingEndDate"       TIMESTAMP(3),
    "injuryDate"           TIMESTAMP(3),
    "injuryDescription"    TEXT,
    "certificationDate"    TIMESTAMP(3),
    "certificationNo"      TEXT,
    "disabilityLevel"      INTEGER,
    "assessmentDate"       TIMESTAMP(3),
    "medicalMonths"        INTEGER,
    "medicalPeriodEnd"     TIMESTAMP(3),
    "description"          TEXT,
    "attachments"          JSONB,
    "timeline"             JSONB,
    "reminderDate"         TIMESTAMP(3),
    "createdBy"            TEXT NOT NULL,
    "createdAt"            TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt"            TIMESTAMP(3) NOT NULL,

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

CREATE INDEX IF NOT EXISTS "EmployeeSpecialStatus_orgId_status_idx" ON "EmployeeSpecialStatus"("orgId", "status");
CREATE INDEX IF NOT EXISTS "EmployeeSpecialStatus_orgId_type_idx" ON "EmployeeSpecialStatus"("orgId", "type");
CREATE INDEX IF NOT EXISTS "EmployeeSpecialStatus_employeeId_idx" ON "EmployeeSpecialStatus"("employeeId");

-- 外键约束
ALTER TABLE "EmployeeSpecialStatus"
  ADD CONSTRAINT "EmployeeSpecialStatus_orgId_fkey"
  FOREIGN KEY ("orgId") REFERENCES "Organization"("id") ON DELETE CASCADE;

ALTER TABLE "EmployeeSpecialStatus"
  ADD CONSTRAINT "EmployeeSpecialStatus_employeeId_fkey"
  FOREIGN KEY ("employeeId") REFERENCES "Employee"("id") ON DELETE CASCADE;
