using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            // Seed account 2300 "Customer Deposits" (Liability / OtherCurrentLiability) for every
            // company that doesn't already have it. Credited when a deposit is taken; debited when
            // the deposit is applied to an invoice. Idempotent guard prevents double-seeding.
            migrationBuilder.Sql(@"
                INSERT INTO Accounts
                    (AccountNumber, Name, AccountType, AccountSubType,
                     IsSystem, IsActive, Description,
                     CompanyId, CreatedAt, IsDeleted,
                     CurrentBalance, OpeningBalance)
                SELECT
                    '2300',
                    'Customer Deposits',
                    2,   -- AccountType.Liability
                    12,  -- AccountSubType.OtherCurrentLiability
                    1,   -- IsSystem = true
                    1,   -- IsActive = true
                    'Deposits received from customers before an invoice is created; cleared when deposit is applied to invoice',
                    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 = '2300'
                      AND a.IsDeleted = 0
                  );
            ");

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 1,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 57, 30, 15, DateTimeKind.Utc).AddTicks(5641));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 2,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 57, 30, 15, DateTimeKind.Utc).AddTicks(5655));

            migrationBuilder.UpdateData(
                table: "PricingTiers",
                keyColumn: "Id",
                keyValue: 3,
                column: "CreatedAt",
                value: new DateTime(2026, 5, 13, 14, 57, 30, 15, DateTimeKind.Utc).AddTicks(5656));
        }

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

            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));
        }
    }
}
