using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PowderCoating.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddAppointmentReminderSentAt : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Use IF EXISTS guards for all ShopWorker drops — prod and dev diverged on whether
            // these objects exist, so unconditional drops would fail on whichever DB is missing them.
            migrationBuilder.Sql(@"
                IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_Jobs_ShopWorkers_ShopWorkerId')
                    ALTER TABLE [Jobs] DROP CONSTRAINT [FK_Jobs_ShopWorkers_ShopWorkerId];
                IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_JobTimeEntries_ShopWorkers_ShopWorkerId')
                    ALTER TABLE [JobTimeEntries] DROP CONSTRAINT [FK_JobTimeEntries_ShopWorkers_ShopWorkerId];
                IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_MaintenanceRecords_ShopWorkers_ShopWorkerId')
                    ALTER TABLE [MaintenanceRecords] DROP CONSTRAINT [FK_MaintenanceRecords_ShopWorkers_ShopWorkerId];
                IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'ShopWorkerRoleCosts')
                    DROP TABLE [ShopWorkerRoleCosts];
                IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'ShopWorkers')
                    DROP TABLE [ShopWorkers];
                IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_MaintenanceRecords_ShopWorkerId' AND object_id = OBJECT_ID('MaintenanceRecords'))
                    DROP INDEX [IX_MaintenanceRecords_ShopWorkerId] ON [MaintenanceRecords];
                IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_JobTimeEntries_ShopWorkerId' AND object_id = OBJECT_ID('JobTimeEntries'))
                    DROP INDEX [IX_JobTimeEntries_ShopWorkerId] ON [JobTimeEntries];
                IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_Jobs_ShopWorkerId' AND object_id = OBJECT_ID('Jobs'))
                    DROP INDEX [IX_Jobs_ShopWorkerId] ON [Jobs];
                IF EXISTS (SELECT 1 FROM sys.columns WHERE name = 'ShopWorkerId' AND object_id = OBJECT_ID('MaintenanceRecords'))
                    ALTER TABLE [MaintenanceRecords] DROP COLUMN [ShopWorkerId];
                IF EXISTS (SELECT 1 FROM sys.columns WHERE name = 'ShopWorkerId' AND object_id = OBJECT_ID('JobTimeEntries'))
                    ALTER TABLE [JobTimeEntries] DROP COLUMN [ShopWorkerId];
                IF EXISTS (SELECT 1 FROM sys.columns WHERE name = 'ShopWorkerId' AND object_id = OBJECT_ID('Jobs'))
                    ALTER TABLE [Jobs] DROP COLUMN [ShopWorkerId];
            ");

            migrationBuilder.AddColumn<DateTime>(
                name: "ReminderSentAt",
                table: "Appointments",
                type: "datetime2",
                nullable: true);

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 19, 15, 12, 57, 355, DateTimeKind.Utc).AddTicks(2970));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 19, 15, 12, 57, 355, DateTimeKind.Utc).AddTicks(2976));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 19, 15, 12, 57, 355, DateTimeKind.Utc).AddTicks(2977));
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "ReminderSentAt",
                table: "Appointments");

            migrationBuilder.AddColumn<int>(
                name: "ShopWorkerId",
                table: "MaintenanceRecords",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "ShopWorkerId",
                table: "JobTimeEntries",
                type: "int",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "ShopWorkerId",
                table: "Jobs",
                type: "int",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "ShopWorkerRoleCosts",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CompanyId = table.Column<int>(type: "int", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    HourlyRate = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    Role = table.Column<int>(type: "int", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ShopWorkerRoleCosts", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ShopWorkers",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CompanyId = table.Column<int>(type: "int", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Notes = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Role = table.Column<int>(type: "int", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ShopWorkers", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ShopWorkers_Companies_CompanyId",
                        column: x => x.CompanyId,
                        principalTable: "Companies",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 15, 23, 44, 10, 471, DateTimeKind.Utc).AddTicks(3131));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 15, 23, 44, 10, 471, DateTimeKind.Utc).AddTicks(3137));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 15, 23, 44, 10, 471, DateTimeKind.Utc).AddTicks(3138));

            migrationBuilder.CreateIndex(
                name: "IX_MaintenanceRecords_ShopWorkerId",
                table: "MaintenanceRecords",
                column: "ShopWorkerId");

            migrationBuilder.CreateIndex(
                name: "IX_JobTimeEntries_ShopWorkerId",
                table: "JobTimeEntries",
                column: "ShopWorkerId");

            migrationBuilder.CreateIndex(
                name: "IX_Jobs_ShopWorkerId",
                table: "Jobs",
                column: "ShopWorkerId");

            migrationBuilder.CreateIndex(
                name: "IX_ShopWorkerRoleCosts_CompanyId_Role",
                table: "ShopWorkerRoleCosts",
                columns: new[] { "CompanyId", "Role" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ShopWorkers_CompanyId",
                table: "ShopWorkers",
                column: "CompanyId");

            migrationBuilder.AddForeignKey(
                name: "FK_Jobs_ShopWorkers_ShopWorkerId",
                table: "Jobs",
                column: "ShopWorkerId",
                principalTable: "ShopWorkers",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_JobTimeEntries_ShopWorkers_ShopWorkerId",
                table: "JobTimeEntries",
                column: "ShopWorkerId",
                principalTable: "ShopWorkers",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_MaintenanceRecords_ShopWorkers_ShopWorkerId",
                table: "MaintenanceRecords",
                column: "ShopWorkerId",
                principalTable: "ShopWorkers",
                principalColumn: "Id");
        }
    }
}
