using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PowderCoating.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AccountingGapsPhase2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "PostedDate",
                table: "VendorCredits",
                type: "datetime2",
                nullable: true);

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

            // Seed the Gift Certificate Liability account (2500) for every company that doesn't
            // already have it. Credit-normal OtherCurrentLiability account; credited when a GC is
            // issued and debited when redeemed or voided. Idempotent guard prevents double-seeding.
            migrationBuilder.Sql(@"
                INSERT INTO Accounts
                    (AccountNumber, Name, AccountType, AccountSubType,
                     IsSystem, IsActive, Description,
                     CompanyId, CreatedAt, IsDeleted,
                     CurrentBalance, OpeningBalance)
                SELECT
                    '2500',
                    'Gift Certificate Liability',
                    2,   -- AccountType.Liability
                    12,  -- AccountSubType.OtherCurrentLiability
                    1,   -- IsSystem = true
                    1,   -- IsActive = true
                    'Outstanding gift certificate obligations owed to certificate holders',
                    c.Id,
                    GETUTCDATE(),
                    0,   -- IsDeleted = false
                    0,   -- CurrentBalance
                    0    -- OpeningBalance
                FROM Companies c
                WHERE c.IsDeleted = 0
                  AND NOT EXISTS (
                    SELECT 1 FROM Accounts a
                    WHERE a.CompanyId = c.Id
                      AND a.AccountNumber = '2500'
                      AND a.IsDeleted = 0
                  );
            ");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 24, 44, 715, DateTimeKind.Utc).AddTicks(9166));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 24, 44, 715, DateTimeKind.Utc).AddTicks(9172));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 24, 44, 715, DateTimeKind.Utc).AddTicks(9174));
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "PostedDate",
                table: "VendorCredits");

            migrationBuilder.DropColumn(
                name: "DepositAccountId",
                table: "Refunds");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 13, 39, 22, 61, DateTimeKind.Utc).AddTicks(8475));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 13, 39, 22, 61, DateTimeKind.Utc).AddTicks(8484));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 13, 39, 22, 61, DateTimeKind.Utc).AddTicks(8486));
        }
    }
}
