-- CreateEnum
CREATE TYPE "WorkspaceConfigStatus" AS ENUM ('draft', 'published', 'archived');

-- CreateEnum
CREATE TYPE "HrFileRecordStatus" AS ENUM ('missing', 'submitted', 'verified', 'rejected');

-- CreateEnum
CREATE TYPE "HrVacancyStatus" AS ENUM ('draft', 'open', 'interviewing', 'offered', 'hired', 'closed', 'cancelled');

-- CreateEnum
CREATE TYPE "HrPayrollFrequency" AS ENUM ('weekly', 'biweekly');

-- CreateEnum
CREATE TYPE "HrPayrollLineType" AS ENUM ('percepciones', 'deducciones', 'aportaciones');

-- CreateEnum
CREATE TYPE "HrImportJobStatus" AS ENUM ('queued', 'success', 'partial', 'failed');

-- CreateEnum
CREATE TYPE "HrCareerContentType" AS ENUM ('announcement', 'course');

-- CreateEnum
CREATE TYPE "HrCareerAssignmentStatus" AS ENUM ('not_started', 'in_progress', 'completed');

-- CreateEnum
CREATE TYPE "HrComplianceBody" AS ENUM ('imss', 'infonavit', 'fonacot', 'other');

-- CreateEnum
CREATE TYPE "HrComplianceStatus" AS ENUM ('on_time', 'due_soon', 'overdue', 'paid', 'blocked');

-- CreateEnum
CREATE TYPE "HrCompliancePaymentStatus" AS ENUM ('scheduled', 'paid', 'late', 'failed');

-- CreateEnum
CREATE TYPE "HrAutomationTaskStatus" AS ENUM ('queued', 'success', 'warning', 'failed');

-- CreateTable
CREATE TABLE "HrWorkspaceConfigVersion" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "version" INTEGER NOT NULL,
    "status" "WorkspaceConfigStatus" NOT NULL DEFAULT 'draft',
    "name" TEXT NOT NULL DEFAULT 'Capital Humano Workspace',
    "tabs" JSONB NOT NULL,
    "changeSummary" TEXT,
    "createdById" TEXT,
    "publishedById" TEXT,
    "publishedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrFileRequirement" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "key" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "description" TEXT,
    "fieldType" TEXT NOT NULL DEFAULT 'document',
    "isRequired" BOOLEAN NOT NULL DEFAULT true,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdById" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrEmployeeFileRecord" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "requirementId" TEXT NOT NULL,
    "status" "HrFileRecordStatus" NOT NULL DEFAULT 'missing',
    "valueText" TEXT,
    "evidenceUrl" TEXT,
    "note" TEXT,
    "verifiedAt" TIMESTAMP(3),
    "verifiedById" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrIdealOrgTarget" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "locationCode" TEXT NOT NULL,
    "roleKey" TEXT NOT NULL,
    "targetCount" INTEGER NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrVacancy" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "locationCode" TEXT NOT NULL,
    "roleKey" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "status" "HrVacancyStatus" NOT NULL DEFAULT 'open',
    "priority" TEXT NOT NULL DEFAULT 'medium',
    "openedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "targetStartAt" TIMESTAMP(3),
    "closedAt" TIMESTAMP(3),
    "hiringManagerUserId" TEXT,
    "ownerUserId" TEXT,
    "notes" TEXT,
    "createdById" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrPayrollRun" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "source" TEXT NOT NULL DEFAULT 'contpaqi_csv',
    "externalRef" TEXT,
    "periodStart" TIMESTAMP(3) NOT NULL,
    "periodEnd" TIMESTAMP(3) NOT NULL,
    "frequency" "HrPayrollFrequency" NOT NULL,
    "locationCode" TEXT,
    "status" TEXT NOT NULL DEFAULT 'imported',
    "rawHash" TEXT,
    "importedById" TEXT,
    "importedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrPayrollLine" (
    "id" TEXT NOT NULL,
    "runId" TEXT NOT NULL,
    "userId" TEXT,
    "personIdentifier" TEXT NOT NULL,
    "lineType" "HrPayrollLineType" NOT NULL,
    "concept" TEXT NOT NULL,
    "amount" DOUBLE PRECISION NOT NULL,
    "currency" TEXT NOT NULL DEFAULT 'MXN',
    "locationCode" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrPayrollImportJob" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "status" "HrImportJobStatus" NOT NULL DEFAULT 'queued',
    "sourceFileName" TEXT,
    "requestHash" TEXT,
    "message" TEXT,
    "rowsRead" INTEGER NOT NULL DEFAULT 0,
    "rowsImported" INTEGER NOT NULL DEFAULT 0,
    "failedRows" INTEGER NOT NULL DEFAULT 0,
    "runId" TEXT,
    "triggeredById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "completedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "HrCareerContent" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "contentType" "HrCareerContentType" NOT NULL,
    "title" TEXT NOT NULL,
    "body" TEXT NOT NULL,
    "sourceDepartment" "DepartmentKey",
    "sourceRef" TEXT,
    "startsAt" TIMESTAMP(3),
    "endsAt" TIMESTAMP(3),
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdById" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrCareerAssignment" (
    "id" TEXT NOT NULL,
    "contentId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "status" "HrCareerAssignmentStatus" NOT NULL DEFAULT 'not_started',
    "progressPct" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "assignedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "dueAt" TIMESTAMP(3),
    "completedAt" TIMESTAMP(3),
    "notes" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrComplianceObligation" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "body" "HrComplianceBody" NOT NULL,
    "title" TEXT NOT NULL,
    "status" "HrComplianceStatus" NOT NULL DEFAULT 'due_soon',
    "locationCode" TEXT,
    "referencePeriod" TEXT,
    "dueDate" TIMESTAMP(3),
    "lastPaymentAt" TIMESTAMP(3),
    "lastPaymentAmount" DOUBLE PRECISION,
    "notes" TEXT,
    "createdById" TEXT,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrCompliancePayment" (
    "id" TEXT NOT NULL,
    "obligationId" TEXT NOT NULL,
    "paymentDate" TIMESTAMP(3) NOT NULL,
    "amount" DOUBLE PRECISION NOT NULL,
    "status" "HrCompliancePaymentStatus" NOT NULL DEFAULT 'paid',
    "referencePeriod" TEXT,
    "receiptUrl" TEXT,
    "note" TEXT,
    "createdById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrAutomationTask" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "taskType" TEXT NOT NULL,
    "status" "HrAutomationTaskStatus" NOT NULL DEFAULT 'queued',
    "title" TEXT NOT NULL,
    "message" TEXT,
    "payload" JSONB,
    "runAt" TIMESTAMP(3) NOT NULL,
    "resolvedAt" TIMESTAMP(3),
    "assignedToId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "HrExceptionQueueItem" (
    "id" TEXT NOT NULL,
    "department" "DepartmentKey" NOT NULL DEFAULT 'capital_humano',
    "source" TEXT NOT NULL,
    "errorCode" TEXT,
    "message" TEXT NOT NULL,
    "payload" JSONB,
    "status" TEXT NOT NULL DEFAULT 'open',
    "retryCount" INTEGER NOT NULL DEFAULT 0,
    "lastRetriedAt" TIMESTAMP(3),
    "resolvedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "HrWorkspaceConfigVersion_department_version_key" ON "HrWorkspaceConfigVersion"("department", "version");

-- CreateIndex
CREATE INDEX "HrWorkspaceConfigVersion_department_status_version_idx" ON "HrWorkspaceConfigVersion"("department", "status", "version");

-- CreateIndex
CREATE UNIQUE INDEX "HrFileRequirement_department_key_key" ON "HrFileRequirement"("department", "key");

-- CreateIndex
CREATE INDEX "HrFileRequirement_department_isActive_sortOrder_idx" ON "HrFileRequirement"("department", "isActive", "sortOrder");

-- CreateIndex
CREATE UNIQUE INDEX "HrEmployeeFileRecord_userId_requirementId_key" ON "HrEmployeeFileRecord"("userId", "requirementId");

-- CreateIndex
CREATE INDEX "HrEmployeeFileRecord_userId_status_idx" ON "HrEmployeeFileRecord"("userId", "status");

-- CreateIndex
CREATE INDEX "HrEmployeeFileRecord_requirementId_status_idx" ON "HrEmployeeFileRecord"("requirementId", "status");

-- CreateIndex
CREATE UNIQUE INDEX "HrIdealOrgTarget_department_locationCode_roleKey_key" ON "HrIdealOrgTarget"("department", "locationCode", "roleKey");

-- CreateIndex
CREATE INDEX "HrIdealOrgTarget_department_locationCode_roleKey_idx" ON "HrIdealOrgTarget"("department", "locationCode", "roleKey");

-- CreateIndex
CREATE INDEX "HrVacancy_department_status_openedAt_idx" ON "HrVacancy"("department", "status", "openedAt");

-- CreateIndex
CREATE INDEX "HrVacancy_department_locationCode_roleKey_idx" ON "HrVacancy"("department", "locationCode", "roleKey");

-- CreateIndex
CREATE INDEX "HrPayrollRun_department_periodStart_periodEnd_idx" ON "HrPayrollRun"("department", "periodStart", "periodEnd");

-- CreateIndex
CREATE INDEX "HrPayrollRun_department_frequency_locationCode_idx" ON "HrPayrollRun"("department", "frequency", "locationCode");

-- CreateIndex
CREATE INDEX "HrPayrollRun_rawHash_idx" ON "HrPayrollRun"("rawHash");

-- CreateIndex
CREATE INDEX "HrPayrollLine_runId_lineType_idx" ON "HrPayrollLine"("runId", "lineType");

-- CreateIndex
CREATE INDEX "HrPayrollLine_userId_idx" ON "HrPayrollLine"("userId");

-- CreateIndex
CREATE INDEX "HrPayrollLine_locationCode_lineType_idx" ON "HrPayrollLine"("locationCode", "lineType");

-- CreateIndex
CREATE INDEX "HrPayrollImportJob_department_status_createdAt_idx" ON "HrPayrollImportJob"("department", "status", "createdAt");

-- CreateIndex
CREATE INDEX "HrPayrollImportJob_requestHash_idx" ON "HrPayrollImportJob"("requestHash");

-- CreateIndex
CREATE INDEX "HrCareerContent_department_contentType_isActive_idx" ON "HrCareerContent"("department", "contentType", "isActive");

-- CreateIndex
CREATE INDEX "HrCareerContent_sourceDepartment_createdAt_idx" ON "HrCareerContent"("sourceDepartment", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "HrCareerAssignment_contentId_userId_key" ON "HrCareerAssignment"("contentId", "userId");

-- CreateIndex
CREATE INDEX "HrCareerAssignment_userId_status_idx" ON "HrCareerAssignment"("userId", "status");

-- CreateIndex
CREATE INDEX "HrComplianceObligation_department_body_status_dueDate_idx" ON "HrComplianceObligation"("department", "body", "status", "dueDate");

-- CreateIndex
CREATE INDEX "HrComplianceObligation_department_locationCode_body_idx" ON "HrComplianceObligation"("department", "locationCode", "body");

-- CreateIndex
CREATE INDEX "HrCompliancePayment_obligationId_paymentDate_idx" ON "HrCompliancePayment"("obligationId", "paymentDate");

-- CreateIndex
CREATE INDEX "HrAutomationTask_department_status_runAt_idx" ON "HrAutomationTask"("department", "status", "runAt");

-- CreateIndex
CREATE INDEX "HrExceptionQueueItem_department_status_createdAt_idx" ON "HrExceptionQueueItem"("department", "status", "createdAt");

-- CreateIndex
CREATE INDEX "HrExceptionQueueItem_source_status_idx" ON "HrExceptionQueueItem"("source", "status");

-- AddForeignKey
ALTER TABLE "HrEmployeeFileRecord" ADD CONSTRAINT "HrEmployeeFileRecord_requirementId_fkey" FOREIGN KEY ("requirementId") REFERENCES "HrFileRequirement"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "HrPayrollLine" ADD CONSTRAINT "HrPayrollLine_runId_fkey" FOREIGN KEY ("runId") REFERENCES "HrPayrollRun"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "HrPayrollImportJob" ADD CONSTRAINT "HrPayrollImportJob_runId_fkey" FOREIGN KEY ("runId") REFERENCES "HrPayrollRun"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "HrCareerAssignment" ADD CONSTRAINT "HrCareerAssignment_contentId_fkey" FOREIGN KEY ("contentId") REFERENCES "HrCareerContent"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "HrCompliancePayment" ADD CONSTRAINT "HrCompliancePayment_obligationId_fkey" FOREIGN KEY ("obligationId") REFERENCES "HrComplianceObligation"("id") ON DELETE CASCADE ON UPDATE CASCADE;
