using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PowderCoating.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddReworkTracking : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "IsReworkJob",
                table: "Jobs",
                type: "bit",
                nullable: false,
                defaultValue: false);

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

            migrationBuilder.CreateTable(
                name: "ReworkRecords",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    JobId = table.Column<int>(type: "int", nullable: false),
                    JobItemId = table.Column<int>(type: "int", nullable: true),
                    ReworkJobId = table.Column<int>(type: "int", nullable: true),
                    ReworkType = table.Column<int>(type: "int", nullable: false),
                    Reason = table.Column<int>(type: "int", nullable: false),
                    DefectDescription = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DiscoveredBy = table.Column<int>(type: "int", nullable: false),
                    DiscoveredDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    ReportedByName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    EstimatedReworkCost = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    ActualReworkCost = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    IsBillableToCustomer = table.Column<bool>(type: "bit", nullable: false),
                    BillingNotes = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Status = table.Column<int>(type: "int", nullable: false),
                    Resolution = table.Column<int>(type: "int", nullable: true),
                    ResolvedDate = table.Column<DateTime>(type: "datetime2", nullable: true),
                    ResolutionNotes = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    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_ReworkRecords", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ReworkRecords_JobItems_JobItemId",
                        column: x => x.JobItemId,
                        principalTable: "JobItems",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_ReworkRecords_Jobs_JobId",
                        column: x => x.JobId,
                        principalTable: "Jobs",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ReworkRecords_Jobs_ReworkJobId",
                        column: x => x.ReworkJobId,
                        principalTable: "Jobs",
                        principalColumn: "Id");
                });

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 42, 32, 909, DateTimeKind.Utc).AddTicks(2998));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 42, 32, 909, DateTimeKind.Utc).AddTicks(3003));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 42, 32, 909, DateTimeKind.Utc).AddTicks(3005));

            migrationBuilder.CreateIndex(
                name: "IX_Jobs_OriginalJobId",
                table: "Jobs",
                column: "OriginalJobId");

            migrationBuilder.CreateIndex(
                name: "IX_ReworkRecords_JobId",
                table: "ReworkRecords",
                column: "JobId");

            migrationBuilder.CreateIndex(
                name: "IX_ReworkRecords_JobItemId",
                table: "ReworkRecords",
                column: "JobItemId");

            migrationBuilder.CreateIndex(
                name: "IX_ReworkRecords_ReworkJobId",
                table: "ReworkRecords",
                column: "ReworkJobId");

            migrationBuilder.AddForeignKey(
                name: "FK_Jobs_Jobs_OriginalJobId",
                table: "Jobs",
                column: "OriginalJobId",
                principalTable: "Jobs",
                principalColumn: "Id");
        }

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

            migrationBuilder.DropTable(
                name: "ReworkRecords");

            migrationBuilder.DropIndex(
                name: "IX_Jobs_OriginalJobId",
                table: "Jobs");

            migrationBuilder.DropColumn(
                name: "IsReworkJob",
                table: "Jobs");

            migrationBuilder.DropColumn(
                name: "OriginalJobId",
                table: "Jobs");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 28, 54, 685, DateTimeKind.Utc).AddTicks(4802));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 28, 54, 685, DateTimeKind.Utc).AddTicks(4849));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 18, 13, 28, 54, 685, DateTimeKind.Utc).AddTicks(4851));
        }
    }
}
