-- CreateEnum
CREATE TYPE "TransferStatus" AS ENUM ('PENDING', 'APPROVED', 'EXECUTED', 'REJECTED', 'CANCELLED');

-- CreateTable
CREATE TABLE "transfer_windows" (
    "id" TEXT NOT NULL,
    "leagueId" TEXT NOT NULL,
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "transfers" (
    "id" TEXT NOT NULL,
    "leagueId" TEXT NOT NULL,
    "playerProfileId" TEXT NOT NULL,
    "fromClubId" TEXT NOT NULL,
    "toClubId" TEXT,
    "freeAgent" BOOLEAN NOT NULL DEFAULT false,
    "notes" TEXT,
    "status" "TransferStatus" NOT NULL DEFAULT 'PENDING',
    "initiatedById" TEXT NOT NULL,
    "approvedById" TEXT,
    "initiatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "approvedAt" TIMESTAMP(3),
    "executedAt" TIMESTAMP(3),

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

-- CreateIndex
CREATE UNIQUE INDEX "transfer_windows_leagueId_key" ON "transfer_windows"("leagueId");

-- CreateIndex
CREATE INDEX "transfer_windows_leagueId_idx" ON "transfer_windows"("leagueId");

-- CreateIndex
CREATE INDEX "transfer_windows_startDate_idx" ON "transfer_windows"("startDate");

-- CreateIndex
CREATE INDEX "transfer_windows_endDate_idx" ON "transfer_windows"("endDate");

-- CreateIndex
CREATE INDEX "transfers_leagueId_idx" ON "transfers"("leagueId");

-- CreateIndex
CREATE INDEX "transfers_fromClubId_idx" ON "transfers"("fromClubId");

-- CreateIndex
CREATE INDEX "transfers_toClubId_idx" ON "transfers"("toClubId");

-- CreateIndex
CREATE INDEX "transfers_playerProfileId_idx" ON "transfers"("playerProfileId");

-- CreateIndex
CREATE INDEX "transfers_status_idx" ON "transfers"("status");

-- CreateIndex
CREATE INDEX "transfers_initiatedAt_idx" ON "transfers"("initiatedAt");

-- AddForeignKey
ALTER TABLE "transfer_windows" ADD CONSTRAINT "transfer_windows_leagueId_fkey" FOREIGN KEY ("leagueId") REFERENCES "leagues"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_leagueId_fkey" FOREIGN KEY ("leagueId") REFERENCES "leagues"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_playerProfileId_fkey" FOREIGN KEY ("playerProfileId") REFERENCES "user_profiles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_fromClubId_fkey" FOREIGN KEY ("fromClubId") REFERENCES "clubs"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_toClubId_fkey" FOREIGN KEY ("toClubId") REFERENCES "clubs"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_initiatedById_fkey" FOREIGN KEY ("initiatedById") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transfers" ADD CONSTRAINT "transfers_approvedById_fkey" FOREIGN KEY ("approvedById") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
