-- CreateEnum
CREATE TYPE "OrganizationDocumentType" AS ENUM ('ACTA_CONSTITUTIVA');

-- AlterTable
ALTER TABLE "Organization"
ADD COLUMN "rfc" TEXT,
ADD COLUMN "legalRepresentative" TEXT,
ADD COLUMN "incorporationDate" TEXT,
ADD COLUMN "deedNumber" TEXT,
ADD COLUMN "notaryName" TEXT,
ADD COLUMN "fiscalAddress" TEXT,
ADD COLUMN "businessPurpose" TEXT,
ADD COLUMN "actaExtractedData" JSONB,
ADD COLUMN "actaUploadedAt" TIMESTAMP(3),
ADD COLUMN "onboardingCompletedAt" TIMESTAMP(3);

-- CreateTable
CREATE TABLE "OrganizationDocument" (
    "id" TEXT NOT NULL,
    "organizationId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" "OrganizationDocumentType" NOT NULL,
    "fileName" TEXT NOT NULL,
    "storedFileName" TEXT NOT NULL,
    "filePath" TEXT NOT NULL,
    "mimeType" TEXT NOT NULL,
    "sizeBytes" INTEGER NOT NULL,
    "checksumSha256" TEXT,
    "extractedData" JSONB,
    "extractedTextSnippet" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "OrganizationDocument_organizationId_type_key" ON "OrganizationDocument"("organizationId", "type");

-- CreateIndex
CREATE UNIQUE INDEX "OrganizationDocument_userId_type_key" ON "OrganizationDocument"("userId", "type");

-- CreateIndex
CREATE INDEX "OrganizationDocument_type_idx" ON "OrganizationDocument"("type");

-- AddForeignKey
ALTER TABLE "OrganizationDocument" ADD CONSTRAINT "OrganizationDocument_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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