DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'CoachStudentBookingStatus') THEN
    CREATE TYPE "CoachStudentBookingStatus" AS ENUM ('SCHEDULED', 'CANCELED');
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'CoachStudentBookingPaymentStatus') THEN
    CREATE TYPE "CoachStudentBookingPaymentStatus" AS ENUM ('PENDING', 'PAID');
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'PlayerGender') THEN
    CREATE TYPE "PlayerGender" AS ENUM ('FEMALE', 'MALE', 'OTHER');
  END IF;
END $$;

ALTER TABLE "Club"
  ALTER COLUMN "name" DROP NOT NULL,
  ALTER COLUMN "slug" DROP NOT NULL,
  ALTER COLUMN "status" DROP NOT NULL,
  ALTER COLUMN "status" DROP DEFAULT;

ALTER TABLE "Coach"
  ADD COLUMN IF NOT EXISTS "assignedCourtId" TEXT;

ALTER TABLE "Player"
  ADD COLUMN IF NOT EXISTS "birthYear" INTEGER,
  ADD COLUMN IF NOT EXISTS "coachCategory" TEXT,
  ADD COLUMN IF NOT EXISTS "gender" "PlayerGender",
  ADD COLUMN IF NOT EXISTS "telegramHandle" TEXT,
  ADD COLUMN IF NOT EXISTS "whatsappPhone" TEXT;

CREATE TABLE IF NOT EXISTS "VenueCoachPriceOverride" (
  "id" TEXT NOT NULL,
  "venueId" TEXT NOT NULL,
  "dayOfWeek" INTEGER NOT NULL,
  "startTime" TEXT NOT NULL,
  "endTime" TEXT NOT NULL,
  "price" INTEGER NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "VenueCoachPriceOverride_pkey" PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "CoachStudentBooking" (
  "id" TEXT NOT NULL,
  "coachId" TEXT NOT NULL,
  "playerId" TEXT NOT NULL,
  "venueId" TEXT NOT NULL,
  "courtId" TEXT,
  "startsAt" TIMESTAMP(3) NOT NULL,
  "endsAt" TIMESTAMP(3) NOT NULL,
  "courtPriceMinor" INTEGER,
  "coachPriceMinor" INTEGER,
  "status" "CoachStudentBookingStatus" NOT NULL DEFAULT 'SCHEDULED',
  "paymentStatus" "CoachStudentBookingPaymentStatus" NOT NULL DEFAULT 'PENDING',
  "note" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "CoachStudentBooking_pkey" PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "VenueCoachPriceOverride_venueId_dayOfWeek_idx"
  ON "VenueCoachPriceOverride"("venueId", "dayOfWeek");

CREATE INDEX IF NOT EXISTS "CoachStudentBooking_coachId_startsAt_idx"
  ON "CoachStudentBooking"("coachId", "startsAt");

CREATE INDEX IF NOT EXISTS "CoachStudentBooking_playerId_startsAt_idx"
  ON "CoachStudentBooking"("playerId", "startsAt");

CREATE INDEX IF NOT EXISTS "CoachStudentBooking_venueId_startsAt_idx"
  ON "CoachStudentBooking"("venueId", "startsAt");

CREATE INDEX IF NOT EXISTS "CoachStudentBooking_courtId_startsAt_idx"
  ON "CoachStudentBooking"("courtId", "startsAt");

CREATE UNIQUE INDEX IF NOT EXISTS "Club_atcSportclubId_key"
  ON "Club"("atcSportclubId");

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'VenueCoachPriceOverride_venueId_fkey') THEN
    ALTER TABLE "VenueCoachPriceOverride"
      ADD CONSTRAINT "VenueCoachPriceOverride_venueId_fkey"
      FOREIGN KEY ("venueId") REFERENCES "Venue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'Coach_assignedCourtId_fkey') THEN
    ALTER TABLE "Coach"
      ADD CONSTRAINT "Coach_assignedCourtId_fkey"
      FOREIGN KEY ("assignedCourtId") REFERENCES "VenueCourt"("id") ON DELETE SET NULL ON UPDATE CASCADE;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'CoachStudentBooking_coachId_fkey') THEN
    ALTER TABLE "CoachStudentBooking"
      ADD CONSTRAINT "CoachStudentBooking_coachId_fkey"
      FOREIGN KEY ("coachId") REFERENCES "Coach"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'CoachStudentBooking_playerId_fkey') THEN
    ALTER TABLE "CoachStudentBooking"
      ADD CONSTRAINT "CoachStudentBooking_playerId_fkey"
      FOREIGN KEY ("playerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'CoachStudentBooking_venueId_fkey') THEN
    ALTER TABLE "CoachStudentBooking"
      ADD CONSTRAINT "CoachStudentBooking_venueId_fkey"
      FOREIGN KEY ("venueId") REFERENCES "Venue"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'CoachStudentBooking_courtId_fkey') THEN
    ALTER TABLE "CoachStudentBooking"
      ADD CONSTRAINT "CoachStudentBooking_courtId_fkey"
      FOREIGN KEY ("courtId") REFERENCES "VenueCourt"("id") ON DELETE SET NULL ON UPDATE CASCADE;
  END IF;
END $$;
