using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RR3CommunityServer.Migrations
{
    /// <inheritdoc />
    public partial class AddEventsSystem : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Events",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    EventCode = table.Column<string>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", nullable: false),
                    SeriesName = table.Column<string>(type: "TEXT", nullable: false),
                    SeriesOrder = table.Column<int>(type: "INTEGER", nullable: false),
                    EventOrder = table.Column<int>(type: "INTEGER", nullable: false),
                    Track = table.Column<string>(type: "TEXT", nullable: false),
                    EventType = table.Column<string>(type: "TEXT", nullable: false),
                    Laps = table.Column<int>(type: "INTEGER", nullable: false),
                    RequiredPR = table.Column<int>(type: "INTEGER", nullable: false),
                    RequiredCarClass = table.Column<string>(type: "TEXT", nullable: true),
                    TargetTime = table.Column<double>(type: "REAL", nullable: true),
                    GoldReward = table.Column<int>(type: "INTEGER", nullable: false),
                    CashReward = table.Column<int>(type: "INTEGER", nullable: false),
                    XPReward = table.Column<int>(type: "INTEGER", nullable: false),
                    Active = table.Column<bool>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Events", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "EventAttempts",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    UserId = table.Column<int>(type: "INTEGER", nullable: false),
                    EventId = table.Column<int>(type: "INTEGER", nullable: false),
                    StartedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Completed = table.Column<bool>(type: "INTEGER", nullable: false),
                    CompletedAt = table.Column<DateTime>(type: "TEXT", nullable: true),
                    TimeSeconds = table.Column<double>(type: "REAL", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EventAttempts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EventAttempts_Events_EventId",
                        column: x => x.EventId,
                        principalTable: "Events",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_EventAttempts_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "EventCompletions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    UserId = table.Column<int>(type: "INTEGER", nullable: false),
                    EventId = table.Column<int>(type: "INTEGER", nullable: false),
                    BestTime = table.Column<double>(type: "REAL", nullable: false),
                    CompletionCount = table.Column<int>(type: "INTEGER", nullable: false),
                    FirstCompletedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    LastCompletedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EventCompletions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EventCompletions_Events_EventId",
                        column: x => x.EventId,
                        principalTable: "Events",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_EventCompletions_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.UpdateData(
                table: "TimeTrials",
                keyColumn: "Id",
                keyValue: 1,
                columns: new[] { "EndDate", "StartDate" },
                values: new object[] { new DateTime(2026, 3, 2, 1, 55, 50, 958, DateTimeKind.Utc).AddTicks(35), new DateTime(2026, 2, 23, 1, 55, 50, 958, DateTimeKind.Utc).AddTicks(32) });

            migrationBuilder.UpdateData(
                table: "TimeTrials",
                keyColumn: "Id",
                keyValue: 2,
                columns: new[] { "EndDate", "StartDate" },
                values: new object[] { new DateTime(2026, 3, 2, 1, 55, 50, 958, DateTimeKind.Utc).AddTicks(43), new DateTime(2026, 2, 23, 1, 55, 50, 958, DateTimeKind.Utc).AddTicks(43) });

            migrationBuilder.CreateIndex(
                name: "IX_TimeTrialResults_TimeTrialId",
                table: "TimeTrialResults",
                column: "TimeTrialId");

            migrationBuilder.CreateIndex(
                name: "IX_TimeTrialResults_UserId",
                table: "TimeTrialResults",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_EventAttempts_EventId",
                table: "EventAttempts",
                column: "EventId");

            migrationBuilder.CreateIndex(
                name: "IX_EventAttempts_UserId",
                table: "EventAttempts",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_EventCompletions_EventId",
                table: "EventCompletions",
                column: "EventId");

            migrationBuilder.CreateIndex(
                name: "IX_EventCompletions_UserId",
                table: "EventCompletions",
                column: "UserId");

            migrationBuilder.AddForeignKey(
                name: "FK_TimeTrialResults_TimeTrials_TimeTrialId",
                table: "TimeTrialResults",
                column: "TimeTrialId",
                principalTable: "TimeTrials",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_TimeTrialResults_Users_UserId",
                table: "TimeTrialResults",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_TimeTrialResults_TimeTrials_TimeTrialId",
                table: "TimeTrialResults");

            migrationBuilder.DropForeignKey(
                name: "FK_TimeTrialResults_Users_UserId",
                table: "TimeTrialResults");

            migrationBuilder.DropTable(
                name: "EventAttempts");

            migrationBuilder.DropTable(
                name: "EventCompletions");

            migrationBuilder.DropTable(
                name: "Events");

            migrationBuilder.DropIndex(
                name: "IX_TimeTrialResults_TimeTrialId",
                table: "TimeTrialResults");

            migrationBuilder.DropIndex(
                name: "IX_TimeTrialResults_UserId",
                table: "TimeTrialResults");

            migrationBuilder.UpdateData(
                table: "TimeTrials",
                keyColumn: "Id",
                keyValue: 1,
                columns: new[] { "EndDate", "StartDate" },
                values: new object[] { new DateTime(2026, 3, 2, 1, 33, 39, 587, DateTimeKind.Utc).AddTicks(7946), new DateTime(2026, 2, 23, 1, 33, 39, 587, DateTimeKind.Utc).AddTicks(7944) });

            migrationBuilder.UpdateData(
                table: "TimeTrials",
                keyColumn: "Id",
                keyValue: 2,
                columns: new[] { "EndDate", "StartDate" },
                values: new object[] { new DateTime(2026, 3, 2, 1, 33, 39, 587, DateTimeKind.Utc).AddTicks(7954), new DateTime(2026, 2, 23, 1, 33, 39, 587, DateTimeKind.Utc).AddTicks(7954) });
        }
    }
}
