using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PowderCoating.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class RenameSupplierToVendor : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Drop all foreign keys that reference the old Suppliers table
            migrationBuilder.DropForeignKey(
                name: "FK_BillPayments_Suppliers_SupplierId",
                table: "BillPayments");

            migrationBuilder.DropForeignKey(
                name: "FK_Bills_Suppliers_SupplierId",
                table: "Bills");

            migrationBuilder.DropForeignKey(
                name: "FK_Expenses_Suppliers_SupplierId",
                table: "Expenses");

            migrationBuilder.DropForeignKey(
                name: "FK_InventoryItems_Suppliers_PrimarySupplierId",
                table: "InventoryItems");

            migrationBuilder.DropForeignKey(
                name: "FK_JobItemCoats_Suppliers_SupplierId",
                table: "JobItemCoats");

            migrationBuilder.DropForeignKey(
                name: "FK_QuoteItemCoats_Suppliers_SupplierId",
                table: "QuoteItemCoats");

            // Rename the table (preserves all data)
            migrationBuilder.RenameTable(
                name: "Suppliers",
                newName: "Vendors");

            // Rename indexes on the Vendors table
            migrationBuilder.RenameIndex(
                name: "IX_Suppliers_CompanyId",
                table: "Vendors",
                newName: "IX_Vendors_CompanyId");

            migrationBuilder.RenameIndex(
                name: "IX_Suppliers_DefaultExpenseAccountId",
                table: "Vendors",
                newName: "IX_Vendors_DefaultExpenseAccountId");

            // Rename columns on child tables
            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "QuoteItemCoats",
                newName: "VendorId");

            migrationBuilder.RenameIndex(
                name: "IX_QuoteItemCoats_SupplierId",
                table: "QuoteItemCoats",
                newName: "IX_QuoteItemCoats_VendorId");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "JobItemCoats",
                newName: "VendorId");

            migrationBuilder.RenameIndex(
                name: "IX_JobItemCoats_SupplierId",
                table: "JobItemCoats",
                newName: "IX_JobItemCoats_VendorId");

            migrationBuilder.RenameColumn(
                name: "SupplierPartNumber",
                table: "InventoryItems",
                newName: "VendorPartNumber");

            migrationBuilder.RenameColumn(
                name: "PrimarySupplierId",
                table: "InventoryItems",
                newName: "PrimaryVendorId");

            migrationBuilder.RenameIndex(
                name: "IX_InventoryItems_PrimarySupplierId",
                table: "InventoryItems",
                newName: "IX_InventoryItems_PrimaryVendorId");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "Expenses",
                newName: "VendorId");

            migrationBuilder.RenameIndex(
                name: "IX_Expenses_SupplierId",
                table: "Expenses",
                newName: "IX_Expenses_VendorId");

            migrationBuilder.RenameColumn(
                name: "SupplierInvoiceNumber",
                table: "Bills",
                newName: "VendorInvoiceNumber");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "Bills",
                newName: "VendorId");

            migrationBuilder.RenameIndex(
                name: "IX_Bills_SupplierId",
                table: "Bills",
                newName: "IX_Bills_VendorId");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "BillPayments",
                newName: "VendorId");

            migrationBuilder.RenameIndex(
                name: "IX_BillPayments_SupplierId",
                table: "BillPayments",
                newName: "IX_BillPayments_VendorId");

            // Rename the user permission column
            migrationBuilder.RenameColumn(
                name: "CanManageSuppliers",
                table: "AspNetUsers",
                newName: "CanManageVendors");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 4, 31, 35, 98, DateTimeKind.Utc).AddTicks(7168));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 4, 31, 35, 98, DateTimeKind.Utc).AddTicks(7176));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 4, 31, 35, 98, DateTimeKind.Utc).AddTicks(7177));

            // Re-add foreign keys pointing to the renamed Vendors table
            migrationBuilder.AddForeignKey(
                name: "FK_BillPayments_Vendors_VendorId",
                table: "BillPayments",
                column: "VendorId",
                principalTable: "Vendors",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_Bills_Vendors_VendorId",
                table: "Bills",
                column: "VendorId",
                principalTable: "Vendors",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Expenses_Vendors_VendorId",
                table: "Expenses",
                column: "VendorId",
                principalTable: "Vendors",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_InventoryItems_Vendors_PrimaryVendorId",
                table: "InventoryItems",
                column: "PrimaryVendorId",
                principalTable: "Vendors",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_JobItemCoats_Vendors_VendorId",
                table: "JobItemCoats",
                column: "VendorId",
                principalTable: "Vendors",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_QuoteItemCoats_Vendors_VendorId",
                table: "QuoteItemCoats",
                column: "VendorId",
                principalTable: "Vendors",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Drop foreign keys pointing to Vendors
            migrationBuilder.DropForeignKey(
                name: "FK_BillPayments_Vendors_VendorId",
                table: "BillPayments");

            migrationBuilder.DropForeignKey(
                name: "FK_Bills_Vendors_VendorId",
                table: "Bills");

            migrationBuilder.DropForeignKey(
                name: "FK_Expenses_Vendors_VendorId",
                table: "Expenses");

            migrationBuilder.DropForeignKey(
                name: "FK_InventoryItems_Vendors_PrimaryVendorId",
                table: "InventoryItems");

            migrationBuilder.DropForeignKey(
                name: "FK_JobItemCoats_Vendors_VendorId",
                table: "JobItemCoats");

            migrationBuilder.DropForeignKey(
                name: "FK_QuoteItemCoats_Vendors_VendorId",
                table: "QuoteItemCoats");

            // Rename Vendors table back to Suppliers
            migrationBuilder.RenameTable(
                name: "Vendors",
                newName: "Suppliers");

            migrationBuilder.RenameIndex(
                name: "IX_Vendors_CompanyId",
                table: "Suppliers",
                newName: "IX_Suppliers_CompanyId");

            migrationBuilder.RenameIndex(
                name: "IX_Vendors_DefaultExpenseAccountId",
                table: "Suppliers",
                newName: "IX_Suppliers_DefaultExpenseAccountId");

            migrationBuilder.RenameColumn(
                name: "VendorId",
                table: "QuoteItemCoats",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_QuoteItemCoats_VendorId",
                table: "QuoteItemCoats",
                newName: "IX_QuoteItemCoats_SupplierId");

            migrationBuilder.RenameColumn(
                name: "VendorId",
                table: "JobItemCoats",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_JobItemCoats_VendorId",
                table: "JobItemCoats",
                newName: "IX_JobItemCoats_SupplierId");

            migrationBuilder.RenameColumn(
                name: "VendorPartNumber",
                table: "InventoryItems",
                newName: "SupplierPartNumber");

            migrationBuilder.RenameColumn(
                name: "PrimaryVendorId",
                table: "InventoryItems",
                newName: "PrimarySupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_InventoryItems_PrimaryVendorId",
                table: "InventoryItems",
                newName: "IX_InventoryItems_PrimarySupplierId");

            migrationBuilder.RenameColumn(
                name: "VendorId",
                table: "Expenses",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_Expenses_VendorId",
                table: "Expenses",
                newName: "IX_Expenses_SupplierId");

            migrationBuilder.RenameColumn(
                name: "VendorInvoiceNumber",
                table: "Bills",
                newName: "SupplierInvoiceNumber");

            migrationBuilder.RenameColumn(
                name: "VendorId",
                table: "Bills",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_Bills_VendorId",
                table: "Bills",
                newName: "IX_Bills_SupplierId");

            migrationBuilder.RenameColumn(
                name: "VendorId",
                table: "BillPayments",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_BillPayments_VendorId",
                table: "BillPayments",
                newName: "IX_BillPayments_SupplierId");

            migrationBuilder.RenameColumn(
                name: "CanManageVendors",
                table: "AspNetUsers",
                newName: "CanManageSuppliers");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 3, 45, 54, 406, DateTimeKind.Utc).AddTicks(8622));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 3, 45, 54, 406, DateTimeKind.Utc).AddTicks(8628));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 3, 8, 3, 45, 54, 406, DateTimeKind.Utc).AddTicks(8630));

            // Re-add foreign keys pointing back to Suppliers
            migrationBuilder.AddForeignKey(
                name: "FK_BillPayments_Suppliers_SupplierId",
                table: "BillPayments",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_Bills_Suppliers_SupplierId",
                table: "Bills",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Expenses_Suppliers_SupplierId",
                table: "Expenses",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_InventoryItems_Suppliers_PrimarySupplierId",
                table: "InventoryItems",
                column: "PrimarySupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_JobItemCoats_Suppliers_SupplierId",
                table: "JobItemCoats",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_QuoteItemCoats_Suppliers_SupplierId",
                table: "QuoteItemCoats",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id");
        }
    }
}
