using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Country ↔ Currency связка + дефолты организации:
    ///   - countries.DefaultCurrencyId (nullable FK → currencies.Id)
    ///   - organizations.DefaultCurrencyId (FK → currencies.Id)
    ///   - organizations.MultiCurrencyEnabled (bool, default false)
    ///   - organizations.DefaultVat (int, default 16)
    /// Seed: KZ→KZT, RU→RUB; org → KZ+KZT.</summary>
    public partial class Phase4_CountryCurrencyOrgDefaults : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.AddColumn<Guid>(
                name: "DefaultCurrencyId", schema: "public", table: "countries",
                type: "uuid", nullable: true);

            b.AddColumn<Guid>(
                name: "DefaultCurrencyId", schema: "public", table: "organizations",
                type: "uuid", nullable: true);
            b.AddColumn<bool>(
                name: "MultiCurrencyEnabled", schema: "public", table: "organizations",
                type: "boolean", nullable: false, defaultValue: false);
            b.AddColumn<int>(
                name: "DefaultVat", schema: "public", table: "organizations",
                type: "integer", nullable: false, defaultValue: 16);

            b.CreateIndex(
                name: "IX_countries_DefaultCurrencyId", schema: "public",
                table: "countries", column: "DefaultCurrencyId");
            b.CreateIndex(
                name: "IX_organizations_DefaultCurrencyId", schema: "public",
                table: "organizations", column: "DefaultCurrencyId");

            b.AddForeignKey(
                name: "FK_countries_currencies_DefaultCurrencyId",
                schema: "public", table: "countries", column: "DefaultCurrencyId",
                principalSchema: "public", principalTable: "currencies", principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            b.AddForeignKey(
                name: "FK_organizations_currencies_DefaultCurrencyId",
                schema: "public", table: "organizations", column: "DefaultCurrencyId",
                principalSchema: "public", principalTable: "currencies", principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            // Backfill: привяжем валюты к странам по ISO-коду.
            b.Sql("""
                UPDATE public.countries SET "DefaultCurrencyId" = c."Id"
                FROM public.currencies c
                WHERE (public.countries."Code" = 'KZ' AND c."Code" = 'KZT')
                   OR (public.countries."Code" = 'RU' AND c."Code" = 'RUB')
                   OR (public.countries."Code" = 'BY' AND c."Code" = 'BYN')
                   OR (public.countries."Code" = 'US' AND c."Code" = 'USD')
                   OR (public.countries."Code" = 'DE' AND c."Code" = 'EUR')
                   OR (public.countries."Code" = 'CN' AND c."Code" = 'CNY')
                   OR (public.countries."Code" = 'TR' AND c."Code" = 'TRY');
            """);

            // Дефолт для организации — KZT, если существует.
            b.Sql("""
                UPDATE public.organizations SET "DefaultCurrencyId" = c."Id"
                FROM public.currencies c
                WHERE c."Code" = 'KZT' AND public.organizations."DefaultCurrencyId" IS NULL;
            """);
        }

        protected override void Down(MigrationBuilder b)
        {
            b.DropForeignKey(name: "FK_countries_currencies_DefaultCurrencyId", schema: "public", table: "countries");
            b.DropForeignKey(name: "FK_organizations_currencies_DefaultCurrencyId", schema: "public", table: "organizations");
            b.DropIndex(name: "IX_countries_DefaultCurrencyId", schema: "public", table: "countries");
            b.DropIndex(name: "IX_organizations_DefaultCurrencyId", schema: "public", table: "organizations");
            b.DropColumn(name: "DefaultCurrencyId", schema: "public", table: "countries");
            b.DropColumn(name: "DefaultCurrencyId", schema: "public", table: "organizations");
            b.DropColumn(name: "MultiCurrencyEnabled", schema: "public", table: "organizations");
            b.DropColumn(name: "DefaultVat", schema: "public", table: "organizations");
        }
    }
}
