CREATE TYPE "public"."event_type" AS ENUM('playoff_game', 'major_tournament', 'race', 'final_standings');--> statement-breakpoint
CREATE TYPE "public"."scoring_pattern" AS ENUM('single_elimination_playoff', 'page_playoff', 'season_standings', 'qualifying_points');--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "event_results" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"scoring_event_id" uuid NOT NULL,
	"participant_id" uuid NOT NULL,
	"placement" integer,
	"qualifying_points_awarded" numeric(10, 2),
	"eliminated" boolean,
	"raw_score" numeric(10, 2),
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "participant_expected_values" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"participant_id" uuid NOT NULL,
	"season_id" uuid NOT NULL,
	"prob_first" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_second" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_third" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_fourth" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_fifth" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_sixth" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_seventh" numeric(5, 2) DEFAULT '0' NOT NULL,
	"prob_eighth" numeric(5, 2) DEFAULT '0' NOT NULL,
	"expected_value" numeric(10, 2) DEFAULT '0' NOT NULL,
	"calculated_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "participant_qualifying_totals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"participant_id" uuid NOT NULL,
	"sports_season_id" uuid NOT NULL,
	"total_qualifying_points" numeric(10, 2) DEFAULT '0' NOT NULL,
	"events_scored" integer DEFAULT 0 NOT NULL,
	"final_ranking" integer,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "participant_season_results" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"participant_id" uuid NOT NULL,
	"sports_season_id" uuid NOT NULL,
	"current_points" numeric(10, 2) DEFAULT '0' NOT NULL,
	"current_position" integer,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "playoff_matches" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"scoring_event_id" uuid NOT NULL,
	"round" varchar(50) NOT NULL,
	"match_number" integer NOT NULL,
	"participant1_id" uuid,
	"participant2_id" uuid,
	"winner_id" uuid,
	"loser_id" uuid,
	"is_complete" boolean DEFAULT false NOT NULL,
	"participant1_score" numeric(10, 2),
	"participant2_score" numeric(10, 2),
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "qualifying_point_config" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"sports_season_id" uuid NOT NULL,
	"placement" integer NOT NULL,
	"points" numeric(10, 2) NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "scoring_events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"sports_season_id" uuid NOT NULL,
	"name" varchar(255) NOT NULL,
	"event_date" date,
	"event_type" "event_type" NOT NULL,
	"playoff_round" varchar(50),
	"is_qualifying_event" boolean DEFAULT false NOT NULL,
	"is_complete" boolean DEFAULT false NOT NULL,
	"completed_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "team_sport_scores" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"team_id" uuid NOT NULL,
	"sports_season_id" uuid NOT NULL,
	"total_points" numeric(10, 2) DEFAULT '0' NOT NULL,
	"participants_completed" integer DEFAULT 0 NOT NULL,
	"participants_total" integer DEFAULT 0 NOT NULL,
	"calculated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "team_standings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"team_id" uuid NOT NULL,
	"season_id" uuid NOT NULL,
	"total_points" numeric(10, 2) DEFAULT '0' NOT NULL,
	"current_rank" integer NOT NULL,
	"previous_rank" integer,
	"first_place_count" integer DEFAULT 0 NOT NULL,
	"second_place_count" integer DEFAULT 0 NOT NULL,
	"third_place_count" integer DEFAULT 0 NOT NULL,
	"fourth_place_count" integer DEFAULT 0 NOT NULL,
	"fifth_place_count" integer DEFAULT 0 NOT NULL,
	"sixth_place_count" integer DEFAULT 0 NOT NULL,
	"seventh_place_count" integer DEFAULT 0 NOT NULL,
	"eighth_place_count" integer DEFAULT 0 NOT NULL,
	"participants_remaining" integer DEFAULT 0 NOT NULL,
	"calculated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "team_standings_snapshots" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"team_id" uuid NOT NULL,
	"season_id" uuid NOT NULL,
	"snapshot_date" date NOT NULL,
	"total_points" numeric(10, 2) DEFAULT '0' NOT NULL,
	"rank" integer NOT NULL,
	"first_place_count" integer DEFAULT 0 NOT NULL,
	"second_place_count" integer DEFAULT 0 NOT NULL,
	"third_place_count" integer DEFAULT 0 NOT NULL,
	"fourth_place_count" integer DEFAULT 0 NOT NULL,
	"fifth_place_count" integer DEFAULT 0 NOT NULL,
	"sixth_place_count" integer DEFAULT 0 NOT NULL,
	"seventh_place_count" integer DEFAULT 0 NOT NULL,
	"eighth_place_count" integer DEFAULT 0 NOT NULL,
	"participants_remaining" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_1st" integer DEFAULT 100 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_2nd" integer DEFAULT 70 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_3rd" integer DEFAULT 50 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_4th" integer DEFAULT 40 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_5th" integer DEFAULT 25 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_6th" integer DEFAULT 25 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_7th" integer DEFAULT 15 NOT NULL;--> statement-breakpoint
ALTER TABLE "seasons" ADD COLUMN "points_for_8th" integer DEFAULT 15 NOT NULL;--> statement-breakpoint
ALTER TABLE "sports_seasons" ADD COLUMN "scoring_pattern" "scoring_pattern";--> statement-breakpoint
ALTER TABLE "sports_seasons" ADD COLUMN "total_majors" integer;--> statement-breakpoint
ALTER TABLE "sports_seasons" ADD COLUMN "majors_completed" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE "sports_seasons" ADD COLUMN "qualifying_points_finalized" boolean DEFAULT false NOT NULL;--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "event_results" ADD CONSTRAINT "event_results_scoring_event_id_scoring_events_id_fk" FOREIGN KEY ("scoring_event_id") REFERENCES "public"."scoring_events"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "event_results" ADD CONSTRAINT "event_results_participant_id_participants_id_fk" FOREIGN KEY ("participant_id") REFERENCES "public"."participants"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_expected_values" ADD CONSTRAINT "participant_expected_values_participant_id_participants_id_fk" FOREIGN KEY ("participant_id") REFERENCES "public"."participants"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_expected_values" ADD CONSTRAINT "participant_expected_values_season_id_seasons_id_fk" FOREIGN KEY ("season_id") REFERENCES "public"."seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_qualifying_totals" ADD CONSTRAINT "participant_qualifying_totals_participant_id_participants_id_fk" FOREIGN KEY ("participant_id") REFERENCES "public"."participants"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_qualifying_totals" ADD CONSTRAINT "participant_qualifying_totals_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_season_results" ADD CONSTRAINT "participant_season_results_participant_id_participants_id_fk" FOREIGN KEY ("participant_id") REFERENCES "public"."participants"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "participant_season_results" ADD CONSTRAINT "participant_season_results_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "playoff_matches" ADD CONSTRAINT "playoff_matches_scoring_event_id_scoring_events_id_fk" FOREIGN KEY ("scoring_event_id") REFERENCES "public"."scoring_events"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "playoff_matches" ADD CONSTRAINT "playoff_matches_participant1_id_participants_id_fk" FOREIGN KEY ("participant1_id") REFERENCES "public"."participants"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "playoff_matches" ADD CONSTRAINT "playoff_matches_participant2_id_participants_id_fk" FOREIGN KEY ("participant2_id") REFERENCES "public"."participants"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "playoff_matches" ADD CONSTRAINT "playoff_matches_winner_id_participants_id_fk" FOREIGN KEY ("winner_id") REFERENCES "public"."participants"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "playoff_matches" ADD CONSTRAINT "playoff_matches_loser_id_participants_id_fk" FOREIGN KEY ("loser_id") REFERENCES "public"."participants"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "qualifying_point_config" ADD CONSTRAINT "qualifying_point_config_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "scoring_events" ADD CONSTRAINT "scoring_events_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_sport_scores" ADD CONSTRAINT "team_sport_scores_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_sport_scores" ADD CONSTRAINT "team_sport_scores_sports_season_id_sports_seasons_id_fk" FOREIGN KEY ("sports_season_id") REFERENCES "public"."sports_seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_standings" ADD CONSTRAINT "team_standings_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_standings" ADD CONSTRAINT "team_standings_season_id_seasons_id_fk" FOREIGN KEY ("season_id") REFERENCES "public"."seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_standings_snapshots" ADD CONSTRAINT "team_standings_snapshots_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "team_standings_snapshots" ADD CONSTRAINT "team_standings_snapshots_season_id_seasons_id_fk" FOREIGN KEY ("season_id") REFERENCES "public"."seasons"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
