using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PowderCoating.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddInventoryCategoryLookup : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "InventoryCategoryId",
                table: "InventoryItems",
                type: "int",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "InventoryCategoryLookups",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CategoryCode = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    DisplayName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DisplayOrder = table.Column<int>(type: "int", nullable: false),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    IsSystemDefined = table.Column<bool>(type: "bit", nullable: false),
                    CompanyId = table.Column<int>(type: "int", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_InventoryCategoryLookups", x => x.Id);
                    table.ForeignKey(
                        name: "FK_InventoryCategoryLookups_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, 2, 14, 4, 18, 25, 882, DateTimeKind.Utc).AddTicks(1520));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 2, 14, 4, 18, 25, 882, DateTimeKind.Utc).AddTicks(1525));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 2, 14, 4, 18, 25, 882, DateTimeKind.Utc).AddTicks(1527));

            // Seed inventory categories for all existing companies
            migrationBuilder.Sql(@"
                INSERT INTO InventoryCategoryLookups (CategoryCode, DisplayName, DisplayOrder, IsActive, IsSystemDefined, CompanyId, CreatedAt, IsDeleted)
                SELECT 'POWDER', 'Powder', 1, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'PRIMER', 'Primer', 2, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'CLEANER', 'Cleaner', 3, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'MASKING', 'Masking Supplies', 4, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'ABRASIVE', 'Abrasive Media', 5, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'CHEMICAL', 'Chemicals', 6, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'CONSUMABLE', 'Consumables', 7, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'TOOL', 'Tools', 8, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
                UNION ALL
                SELECT 'OTHER', 'Other', 9, 1, 1, c.Id, GETUTCDATE(), 0 FROM Companies c
            ");

            // Map existing inventory items to the new lookup table based on their Category string value
            migrationBuilder.Sql(@"
                UPDATE i
                SET i.InventoryCategoryId = c.Id
                FROM InventoryItems i
                INNER JOIN InventoryCategoryLookups c ON i.CompanyId = c.CompanyId
                WHERE i.Category = 'Powder' AND c.CategoryCode = 'POWDER'
                   OR i.Category = 'Primer' AND c.CategoryCode = 'PRIMER'
                   OR i.Category = 'Cleaner' AND c.CategoryCode = 'CLEANER'
                   OR i.Category = 'Masking' AND c.CategoryCode = 'MASKING'
                   OR i.Category = 'Abrasive' AND c.CategoryCode = 'ABRASIVE'
                   OR i.Category = 'Chemical' AND c.CategoryCode = 'CHEMICAL'
                   OR i.Category = 'Consumable' AND c.CategoryCode = 'CONSUMABLE'
                   OR i.Category = 'Tool' AND c.CategoryCode = 'TOOL'
                   OR i.Category = 'Other' AND c.CategoryCode = 'OTHER'
            ");

            migrationBuilder.CreateIndex(
                name: "IX_InventoryItems_InventoryCategoryId",
                table: "InventoryItems",
                column: "InventoryCategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_InventoryCategoryLookups_CompanyId",
                table: "InventoryCategoryLookups",
                column: "CompanyId");

            migrationBuilder.CreateIndex(
                name: "IX_InventoryCategoryLookups_CompanyId_CategoryCode",
                table: "InventoryCategoryLookups",
                columns: new[] { "CompanyId", "CategoryCode" },
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_InventoryItems_InventoryCategoryLookups_InventoryCategoryId",
                table: "InventoryItems",
                column: "InventoryCategoryId",
                principalTable: "InventoryCategoryLookups",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropTable(
                name: "InventoryCategoryLookups");

            migrationBuilder.DropIndex(
                name: "IX_InventoryItems_InventoryCategoryId",
                table: "InventoryItems");

            migrationBuilder.DropColumn(
                name: "InventoryCategoryId",
                table: "InventoryItems");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 2, 14, 2, 38, 28, 726, DateTimeKind.Utc).AddTicks(6427));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 2, 14, 2, 38, 28, 726, DateTimeKind.Utc).AddTicks(6433));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 2, 14, 2, 38, 28, 726, DateTimeKind.Utc).AddTicks(6437));
        }
    }
}
