using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

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

            migrationBuilder.CreateTable(
                name: "FormulaLibraryItems",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    OutputMode = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    FieldsJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Formula = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DefaultRate = table.Column<decimal>(type: "decimal(18,2)", nullable: true),
                    RateLabel = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Notes = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DiagramImagePath = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Tags = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IndustryHint = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    SourceCustomItemTemplateId = table.Column<int>(type: "int", nullable: false),
                    SourceCompanyId = table.Column<int>(type: "int", nullable: false),
                    SourceCompanyName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    InspiredByFormulaLibraryItemId = table.Column<int>(type: "int", nullable: true),
                    SharedByUserId = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    SharedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsPublished = table.Column<bool>(type: "bit", nullable: false),
                    ImportCount = table.Column<int>(type: "int", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FormulaLibraryItems", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FormulaLibraryItems_FormulaLibraryItems_InspiredByFormulaLibraryItemId",
                        column: x => x.InspiredByFormulaLibraryItemId,
                        principalTable: "FormulaLibraryItems",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "FormulaLibraryImports",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FormulaLibraryItemId = table.Column<int>(type: "int", nullable: false),
                    ImportedByUserId = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ImportedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    ResultingCustomItemTemplateId = 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_FormulaLibraryImports", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FormulaLibraryImports_CustomItemTemplates_ResultingCustomItemTemplateId",
                        column: x => x.ResultingCustomItemTemplateId,
                        principalTable: "CustomItemTemplates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_FormulaLibraryImports_FormulaLibraryItems_FormulaLibraryItemId",
                        column: x => x.FormulaLibraryItemId,
                        principalTable: "FormulaLibraryItems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 28, 1, 1, 15, 582, DateTimeKind.Utc).AddTicks(3849));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 28, 1, 1, 15, 582, DateTimeKind.Utc).AddTicks(3855));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 28, 1, 1, 15, 582, DateTimeKind.Utc).AddTicks(3856));

            migrationBuilder.CreateIndex(
                name: "IX_CustomItemTemplates_SourceFormulaLibraryItemId",
                table: "CustomItemTemplates",
                column: "SourceFormulaLibraryItemId");

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryImports_Company_Item",
                table: "FormulaLibraryImports",
                columns: new[] { "CompanyId", "FormulaLibraryItemId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryImports_FormulaLibraryItemId",
                table: "FormulaLibraryImports",
                column: "FormulaLibraryItemId");

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryImports_ResultingCustomItemTemplateId",
                table: "FormulaLibraryImports",
                column: "ResultingCustomItemTemplateId");

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryItems_InspiredByFormulaLibraryItemId",
                table: "FormulaLibraryItems",
                column: "InspiredByFormulaLibraryItemId");

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryItems_IsPublished",
                table: "FormulaLibraryItems",
                column: "IsPublished");

            migrationBuilder.CreateIndex(
                name: "IX_FormulaLibraryItems_SourceCompanyId",
                table: "FormulaLibraryItems",
                column: "SourceCompanyId");

            migrationBuilder.AddForeignKey(
                name: "FK_CustomItemTemplates_FormulaLibraryItems_SourceFormulaLibraryItemId",
                table: "CustomItemTemplates",
                column: "SourceFormulaLibraryItemId",
                principalTable: "FormulaLibraryItems",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);
        }

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

            migrationBuilder.DropTable(
                name: "FormulaLibraryImports");

            migrationBuilder.DropTable(
                name: "FormulaLibraryItems");

            migrationBuilder.DropIndex(
                name: "IX_CustomItemTemplates_SourceFormulaLibraryItemId",
                table: "CustomItemTemplates");

            migrationBuilder.DropColumn(
                name: "IsModifiedFromSource",
                table: "CustomItemTemplates");

            migrationBuilder.DropColumn(
                name: "SourceFormulaLibraryItemId",
                table: "CustomItemTemplates");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 27, 13, 46, 47, 552, DateTimeKind.Utc).AddTicks(8956));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 27, 13, 46, 47, 552, DateTimeKind.Utc).AddTicks(8962));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 27, 13, 46, 47, 552, DateTimeKind.Utc).AddTicks(8964));
        }
    }
}
