-- CreateTable
CREATE TABLE "divisions" (
    "id" SERIAL NOT NULL,
    "name" VARCHAR(50) NOT NULL,
    "abbreviation" VARCHAR(3) NOT NULL,
    "display_order" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "competition_types" (
    "id" SERIAL NOT NULL,
    "name" VARCHAR(50) NOT NULL,
    "abbreviation" VARCHAR(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "event_user_heats" (
    "id" SERIAL NOT NULL,
    "user_id" INTEGER NOT NULL,
    "event_id" INTEGER NOT NULL,
    "division_id" INTEGER NOT NULL,
    "competition_type_id" INTEGER NOT NULL,
    "heat_number" INTEGER NOT NULL,
    "role" VARCHAR(10),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "divisions_name_key" ON "divisions"("name");

-- CreateIndex
CREATE UNIQUE INDEX "divisions_abbreviation_key" ON "divisions"("abbreviation");

-- CreateIndex
CREATE UNIQUE INDEX "competition_types_name_key" ON "competition_types"("name");

-- CreateIndex
CREATE UNIQUE INDEX "competition_types_abbreviation_key" ON "competition_types"("abbreviation");

-- CreateIndex
CREATE INDEX "event_user_heats_user_id_event_id_idx" ON "event_user_heats"("user_id", "event_id");

-- CreateIndex
CREATE INDEX "event_user_heats_event_id_idx" ON "event_user_heats"("event_id");

-- CreateIndex
CREATE UNIQUE INDEX "event_user_heats_user_id_event_id_division_id_competition_t_key" ON "event_user_heats"("user_id", "event_id", "division_id", "competition_type_id", "role");

-- AddForeignKey
ALTER TABLE "event_user_heats" ADD CONSTRAINT "event_user_heats_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "event_user_heats" ADD CONSTRAINT "event_user_heats_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "event_user_heats" ADD CONSTRAINT "event_user_heats_division_id_fkey" FOREIGN KEY ("division_id") REFERENCES "divisions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "event_user_heats" ADD CONSTRAINT "event_user_heats_competition_type_id_fkey" FOREIGN KEY ("competition_type_id") REFERENCES "competition_types"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
