using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.AddColumn<bool>(
                name: "IncludePrepCost",
                table: "JobItems",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "IsGenericItem",
                table: "JobItems",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<bool>(
                name: "IsLaborItem",
                table: "JobItems",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<decimal>(
                name: "ManualUnitPrice",
                table: "JobItems",
                type: "decimal(18,2)",
                nullable: true);

            migrationBuilder.AddColumn<decimal>(
                name: "SurfaceAreaSqFt",
                table: "JobItems",
                type: "decimal(18,2)",
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.CreateTable(
                name: "JobItemPrepServices",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    JobItemId = table.Column<int>(type: "int", nullable: false),
                    PrepServiceId = table.Column<int>(type: "int", nullable: false),
                    EstimatedMinutes = table.Column<int>(type: "int", 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_JobItemPrepServices", x => x.Id);
                    table.ForeignKey(
                        name: "FK_JobItemPrepServices_Companies_CompanyId",
                        column: x => x.CompanyId,
                        principalTable: "Companies",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_JobItemPrepServices_JobItems_JobItemId",
                        column: x => x.JobItemId,
                        principalTable: "JobItems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_JobItemPrepServices_PrepServices_PrepServiceId",
                        column: x => x.PrepServiceId,
                        principalTable: "PrepServices",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 4, 22, 49, 769, DateTimeKind.Utc).AddTicks(4725));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 4, 22, 49, 769, DateTimeKind.Utc).AddTicks(4732));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 4, 22, 49, 769, DateTimeKind.Utc).AddTicks(4733));

            migrationBuilder.CreateIndex(
                name: "IX_JobItems_CatalogItemId",
                table: "JobItems",
                column: "CatalogItemId");

            migrationBuilder.CreateIndex(
                name: "IX_JobItemPrepServices_CompanyId",
                table: "JobItemPrepServices",
                column: "CompanyId");

            migrationBuilder.CreateIndex(
                name: "IX_JobItemPrepServices_JobItemId",
                table: "JobItemPrepServices",
                column: "JobItemId");

            migrationBuilder.CreateIndex(
                name: "IX_JobItemPrepServices_PrepServiceId",
                table: "JobItemPrepServices",
                column: "PrepServiceId");

            migrationBuilder.AddForeignKey(
                name: "FK_JobItems_CatalogItems_CatalogItemId",
                table: "JobItems",
                column: "CatalogItemId",
                principalTable: "CatalogItems",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);
        }

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

            migrationBuilder.DropTable(
                name: "JobItemPrepServices");

            migrationBuilder.DropIndex(
                name: "IX_JobItems_CatalogItemId",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "CatalogItemId",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "IncludePrepCost",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "IsGenericItem",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "IsLaborItem",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "ManualUnitPrice",
                table: "JobItems");

            migrationBuilder.DropColumn(
                name: "SurfaceAreaSqFt",
                table: "JobItems");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 2, 52, 50, 21, DateTimeKind.Utc).AddTicks(3171));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 2, 52, 50, 21, DateTimeKind.Utc).AddTicks(3179));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 7, 2, 52, 50, 21, DateTimeKind.Utc).AddTicks(3180));
        }
    }
}
