-- CreateEnum
CREATE TYPE "LicitationSource" AS ENUM ('PNT', 'MUNICIPAL_BACKUP');

-- CreateEnum
CREATE TYPE "LicitationProcedureType" AS ENUM ('LICITACION_PUBLICA', 'INVITACION_RESTRINGIDA', 'ADJUDICACION_DIRECTA', 'UNKNOWN');

-- CreateEnum
CREATE TYPE "LicitationCategory" AS ENUM ('GOODS', 'SERVICES', 'WORKS', 'MIXED', 'UNKNOWN');

-- CreateEnum
CREATE TYPE "SyncRunStatus" AS ENUM ('SUCCESS', 'PARTIAL', 'FAILED');

-- CreateTable
CREATE TABLE "Municipality" (
    "id" TEXT NOT NULL,
    "stateCode" TEXT NOT NULL,
    "stateName" TEXT NOT NULL,
    "municipalityCode" TEXT NOT NULL,
    "municipalityName" TEXT NOT NULL,
    "pntSubjectId" TEXT,
    "pntEntityId" TEXT,
    "pntSectorId" TEXT,
    "pntEntryUrl" TEXT,
    "backupUrl" TEXT,
    "scrapingEnabled" BOOLEAN NOT NULL DEFAULT true,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Licitation" (
    "id" TEXT NOT NULL,
    "municipalityId" TEXT NOT NULL,
    "source" "LicitationSource" NOT NULL,
    "sourceRecordId" TEXT NOT NULL,
    "procedureType" "LicitationProcedureType" NOT NULL DEFAULT 'UNKNOWN',
    "title" TEXT NOT NULL,
    "description" TEXT,
    "category" "LicitationCategory" DEFAULT 'UNKNOWN',
    "publishDate" TIMESTAMP(3),
    "eventDates" JSONB,
    "amount" DECIMAL(14,2),
    "currency" TEXT,
    "status" TEXT,
    "supplierAwarded" TEXT,
    "documents" JSONB,
    "rawSourceUrl" TEXT,
    "rawPayload" JSONB NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SyncRun" (
    "id" TEXT NOT NULL,
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "finishedAt" TIMESTAMP(3),
    "municipalityId" TEXT,
    "source" "LicitationSource" NOT NULL,
    "status" "SyncRunStatus" NOT NULL DEFAULT 'SUCCESS',
    "stats" JSONB,
    "error" TEXT,

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

-- CreateTable
CREATE TABLE "CompanyProfile" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "organizationId" TEXT,
    "locations" JSONB,
    "categoriesSupported" JSONB,
    "keywords" JSONB,
    "minAmount" DECIMAL(14,2),
    "maxAmount" DECIMAL(14,2),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Municipality_stateCode_municipalityCode_key" ON "Municipality"("stateCode", "municipalityCode");

-- CreateIndex
CREATE INDEX "Municipality_stateCode_municipalityName_idx" ON "Municipality"("stateCode", "municipalityName");

-- CreateIndex
CREATE INDEX "Municipality_isActive_scrapingEnabled_idx" ON "Municipality"("isActive", "scrapingEnabled");

-- CreateIndex
CREATE UNIQUE INDEX "Licitation_municipalityId_source_sourceRecordId_key" ON "Licitation"("municipalityId", "source", "sourceRecordId");

-- CreateIndex
CREATE INDEX "Licitation_municipalityId_publishDate_idx" ON "Licitation"("municipalityId", "publishDate");

-- CreateIndex
CREATE INDEX "Licitation_procedureType_category_idx" ON "Licitation"("procedureType", "category");

-- CreateIndex
CREATE INDEX "Licitation_amount_idx" ON "Licitation"("amount");

-- CreateIndex
CREATE INDEX "Licitation_createdAt_idx" ON "Licitation"("createdAt");

-- CreateIndex
CREATE INDEX "SyncRun_municipalityId_startedAt_idx" ON "SyncRun"("municipalityId", "startedAt");

-- CreateIndex
CREATE INDEX "SyncRun_source_status_startedAt_idx" ON "SyncRun"("source", "status", "startedAt");

-- CreateIndex
CREATE UNIQUE INDEX "CompanyProfile_userId_key" ON "CompanyProfile"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "CompanyProfile_organizationId_key" ON "CompanyProfile"("organizationId");

-- AddForeignKey
ALTER TABLE "Licitation" ADD CONSTRAINT "Licitation_municipalityId_fkey" FOREIGN KEY ("municipalityId") REFERENCES "Municipality"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SyncRun" ADD CONSTRAINT "SyncRun_municipalityId_fkey" FOREIGN KEY ("municipalityId") REFERENCES "Municipality"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CompanyProfile" ADD CONSTRAINT "CompanyProfile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CompanyProfile" ADD CONSTRAINT "CompanyProfile_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE SET NULL ON UPDATE CASCADE;
