-- CreateEnum
CREATE TYPE "Role" AS ENUM ('PRIMARY', 'SUB', 'TRAINEE');

-- CreateTable
CREATE TABLE "YearRate" (
    "id" TEXT NOT NULL,
    "year" INTEGER NOT NULL,
    "rehearsalBase" DECIMAL(10,2) NOT NULL,
    "performanceBase" DECIMAL(10,2) NOT NULL,
    "rehearsalOTMultiplier" DECIMAL(4,2) NOT NULL DEFAULT 1.50,
    "onstagePct" DECIMAL(6,4),
    "costumePct" DECIMAL(6,4),
    "memorizeAPct" DECIMAL(6,4),
    "memorizeBPct" DECIMAL(6,4),
    "choreographyPct" DECIMAL(6,4),
    "double1Pct" DECIMAL(6,4),
    "double2Pct" DECIMAL(6,4),
    "double3Pct" DECIMAL(6,4),
    "double4Pct" DECIMAL(6,4),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Production" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "startDate" TIMESTAMP(3),
    "yearRateId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BandTrack" (
    "id" TEXT NOT NULL,
    "productionId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "instrument" TEXT,

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

-- CreateTable
CREATE TABLE "EngagementWeek" (
    "id" TEXT NOT NULL,
    "productionId" TEXT NOT NULL,
    "weekStart" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Musician" (
    "id" TEXT NOT NULL,
    "firstName" TEXT NOT NULL,
    "lastName" TEXT NOT NULL,
    "email" TEXT,
    "unionId" TEXT,
    "active" BOOLEAN NOT NULL DEFAULT true,

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

-- CreateTable
CREATE TABLE "Assignment" (
    "id" TEXT NOT NULL,
    "engagementWeekId" TEXT NOT NULL,
    "bandTrackId" TEXT NOT NULL,
    "musicianId" TEXT NOT NULL,
    "role" "Role" NOT NULL,

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

-- CreateTable
CREATE TABLE "TimeEntry" (
    "id" TEXT NOT NULL,
    "engagementWeekId" TEXT NOT NULL,
    "bandTrackId" TEXT NOT NULL,
    "rehHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "rehOTHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "onstageHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "onstageOTHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "costumeHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "costumeOTHours" DECIMAL(6,2) NOT NULL DEFAULT 0,
    "performances" INTEGER NOT NULL DEFAULT 0,
    "performanceOTMinutes" INTEGER NOT NULL DEFAULT 0,
    "applyOnstage" BOOLEAN NOT NULL DEFAULT false,
    "applyCostume" BOOLEAN NOT NULL DEFAULT false,
    "applyMemorizeA" BOOLEAN NOT NULL DEFAULT false,
    "applyMemorizeB" BOOLEAN NOT NULL DEFAULT false,
    "applyChoreography" BOOLEAN NOT NULL DEFAULT false,
    "doublesCount" INTEGER NOT NULL DEFAULT 0,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "YearRate_year_key" ON "YearRate"("year");

-- CreateIndex
CREATE INDEX "Production_yearRateId_idx" ON "Production"("yearRateId");

-- CreateIndex
CREATE UNIQUE INDEX "Production_name_yearRateId_key" ON "Production"("name", "yearRateId");

-- CreateIndex
CREATE INDEX "BandTrack_productionId_idx" ON "BandTrack"("productionId");

-- CreateIndex
CREATE UNIQUE INDEX "BandTrack_productionId_name_key" ON "BandTrack"("productionId", "name");

-- CreateIndex
CREATE INDEX "EngagementWeek_productionId_weekStart_idx" ON "EngagementWeek"("productionId", "weekStart");

-- CreateIndex
CREATE UNIQUE INDEX "EngagementWeek_productionId_weekStart_key" ON "EngagementWeek"("productionId", "weekStart");

-- CreateIndex
CREATE UNIQUE INDEX "Musician_email_key" ON "Musician"("email");

-- CreateIndex
CREATE INDEX "Musician_lastName_firstName_idx" ON "Musician"("lastName", "firstName");

-- CreateIndex
CREATE INDEX "Assignment_musicianId_idx" ON "Assignment"("musicianId");

-- CreateIndex
CREATE UNIQUE INDEX "Assignment_engagementWeekId_bandTrackId_role_key" ON "Assignment"("engagementWeekId", "bandTrackId", "role");

-- CreateIndex
CREATE INDEX "TimeEntry_engagementWeekId_bandTrackId_idx" ON "TimeEntry"("engagementWeekId", "bandTrackId");

-- CreateIndex
CREATE UNIQUE INDEX "TimeEntry_engagementWeekId_bandTrackId_key" ON "TimeEntry"("engagementWeekId", "bandTrackId");

-- AddForeignKey
ALTER TABLE "Production" ADD CONSTRAINT "Production_yearRateId_fkey" FOREIGN KEY ("yearRateId") REFERENCES "YearRate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BandTrack" ADD CONSTRAINT "BandTrack_productionId_fkey" FOREIGN KEY ("productionId") REFERENCES "Production"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "EngagementWeek" ADD CONSTRAINT "EngagementWeek_productionId_fkey" FOREIGN KEY ("productionId") REFERENCES "Production"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Assignment" ADD CONSTRAINT "Assignment_engagementWeekId_fkey" FOREIGN KEY ("engagementWeekId") REFERENCES "EngagementWeek"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Assignment" ADD CONSTRAINT "Assignment_bandTrackId_fkey" FOREIGN KEY ("bandTrackId") REFERENCES "BandTrack"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Assignment" ADD CONSTRAINT "Assignment_musicianId_fkey" FOREIGN KEY ("musicianId") REFERENCES "Musician"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TimeEntry" ADD CONSTRAINT "TimeEntry_bandTrackId_fkey" FOREIGN KEY ("bandTrackId") REFERENCES "BandTrack"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TimeEntry" ADD CONSTRAINT "TimeEntry_engagementWeekId_fkey" FOREIGN KEY ("engagementWeekId") REFERENCES "EngagementWeek"("id") ON DELETE CASCADE ON UPDATE CASCADE;
