DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'MeetingResponseStatus') THEN
    CREATE TYPE "public"."MeetingResponseStatus" AS ENUM ('pending', 'accepted', 'declined');
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'ProjectCalendarVisibility') THEN
    CREATE TYPE "public"."ProjectCalendarVisibility" AS ENUM ('personal', 'team');
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'ProjectCaptureEvidenceKind') THEN
    CREATE TYPE "public"."ProjectCaptureEvidenceKind" AS ENUM ('photo', 'document', 'link');
  END IF;
END $$;

CREATE TABLE IF NOT EXISTS "public"."MarketingMeetingParticipant" (
  "id" TEXT NOT NULL,
  "meetingId" TEXT NOT NULL,
  "userId" TEXT,
  "displayName" TEXT NOT NULL,
  "responseStatus" "public"."MeetingResponseStatus" NOT NULL DEFAULT 'pending',
  "respondedAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

  CONSTRAINT "MarketingMeetingParticipant_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "MarketingMeetingParticipant_meetingId_fkey" FOREIGN KEY ("meetingId") REFERENCES "public"."MarketingMeeting"("id") ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT "MarketingMeetingParticipant_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS "public"."ProjectCalendarEvent" (
  "id" TEXT NOT NULL,
  "department" "public"."DepartmentKey" NOT NULL DEFAULT 'proyectos',
  "ownerUserId" TEXT NOT NULL,
  "meetingId" TEXT,
  "title" TEXT NOT NULL,
  "notes" TEXT,
  "startAt" TIMESTAMP(3) NOT NULL,
  "endAt" TIMESTAMP(3) NOT NULL,
  "visibility" "public"."ProjectCalendarVisibility" NOT NULL DEFAULT 'personal',
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

  CONSTRAINT "ProjectCalendarEvent_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "ProjectCalendarEvent_ownerUserId_fkey" FOREIGN KEY ("ownerUserId") REFERENCES "public"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT "ProjectCalendarEvent_meetingId_fkey" FOREIGN KEY ("meetingId") REFERENCES "public"."MarketingMeeting"("id") ON DELETE SET NULL ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS "public"."ProjectCaptureEvidence" (
  "id" TEXT NOT NULL,
  "department" "public"."DepartmentKey" NOT NULL DEFAULT 'proyectos',
  "initiativeId" TEXT,
  "taskId" TEXT,
  "uploadedById" TEXT NOT NULL,
  "kind" "public"."ProjectCaptureEvidenceKind" NOT NULL,
  "title" TEXT NOT NULL,
  "note" TEXT,
  "url" TEXT,
  "storagePath" TEXT,
  "mimeType" TEXT,
  "sizeBytes" INTEGER,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

  CONSTRAINT "ProjectCaptureEvidence_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "ProjectCaptureEvidence_initiativeId_fkey" FOREIGN KEY ("initiativeId") REFERENCES "public"."MarketingInitiative"("id") ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT "ProjectCaptureEvidence_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "public"."MarketingTask"("id") ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT "ProjectCaptureEvidence_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "public"."User"("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

CREATE UNIQUE INDEX IF NOT EXISTS "MarketingMeetingParticipant_meetingId_userId_key" ON "public"."MarketingMeetingParticipant"("meetingId", "userId");
CREATE INDEX IF NOT EXISTS "MarketingMeetingParticipant_meetingId_responseStatus_idx" ON "public"."MarketingMeetingParticipant"("meetingId", "responseStatus");
CREATE INDEX IF NOT EXISTS "MarketingMeetingParticipant_userId_idx" ON "public"."MarketingMeetingParticipant"("userId");

CREATE UNIQUE INDEX IF NOT EXISTS "ProjectCalendarEvent_meetingId_key" ON "public"."ProjectCalendarEvent"("meetingId");
CREATE INDEX IF NOT EXISTS "ProjectCalendarEvent_department_startAt_endAt_idx" ON "public"."ProjectCalendarEvent"("department", "startAt", "endAt");
CREATE INDEX IF NOT EXISTS "ProjectCalendarEvent_ownerUserId_startAt_idx" ON "public"."ProjectCalendarEvent"("ownerUserId", "startAt");

CREATE INDEX IF NOT EXISTS "ProjectCaptureEvidence_department_createdAt_idx" ON "public"."ProjectCaptureEvidence"("department", "createdAt");
CREATE INDEX IF NOT EXISTS "ProjectCaptureEvidence_initiativeId_createdAt_idx" ON "public"."ProjectCaptureEvidence"("initiativeId", "createdAt");
CREATE INDEX IF NOT EXISTS "ProjectCaptureEvidence_taskId_createdAt_idx" ON "public"."ProjectCaptureEvidence"("taskId", "createdAt");
CREATE INDEX IF NOT EXISTS "ProjectCaptureEvidence_uploadedById_createdAt_idx" ON "public"."ProjectCaptureEvidence"("uploadedById", "createdAt");
