using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using foodmarket.Infrastructure.Persistence;

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase8a — оптовая отгрузка (Demand).
    ///
    /// По MoySklad: «Отгрузка контрагенту». Зеркалит RetailSale но всегда с
    /// юрлицом-контрагентом, способ оплаты включает Credit (постоплата).
    /// При проведении создаёт stock_movements тип WholesaleSale (=3) с -Quantity.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528200000_Phase8a_Demands")]
    public partial class Phase8a_Demands : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.demands (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""Number""           varchar(50) NOT NULL,
                    ""Date""             timestamp with time zone NOT NULL,
                    ""Status""           integer NOT NULL,
                    ""CustomerId""       uuid NOT NULL,
                    ""StoreId""          uuid NOT NULL,
                    ""CurrencyId""       uuid NOT NULL,
                    ""Payment""          integer NOT NULL,
                    ""Subtotal""         numeric(18,4) NOT NULL,
                    ""DiscountTotal""    numeric(18,4) NOT NULL,
                    ""Total""            numeric(18,4) NOT NULL,
                    ""PaidAmount""       numeric(18,4) NOT NULL DEFAULT 0,
                    ""Notes""            varchar(1000),
                    ""PostedAt""         timestamp with time zone,
                    ""PostedByUserId""   uuid,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_demands_counterparties_CustomerId"" FOREIGN KEY (""CustomerId"") REFERENCES public.counterparties(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_demands_stores_StoreId"" FOREIGN KEY (""StoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_demands_currencies_CurrencyId"" FOREIGN KEY (""CurrencyId"") REFERENCES public.currencies(""Id"") ON DELETE RESTRICT
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_demands_OrganizationId_Number"" ON public.demands (""OrganizationId"", ""Number"");
                CREATE INDEX IF NOT EXISTS ""IX_demands_OrganizationId_Date"" ON public.demands (""OrganizationId"", ""Date"");
                CREATE INDEX IF NOT EXISTS ""IX_demands_OrganizationId_Status"" ON public.demands (""OrganizationId"", ""Status"");
                CREATE INDEX IF NOT EXISTS ""IX_demands_OrganizationId_CustomerId"" ON public.demands (""OrganizationId"", ""CustomerId"");
                CREATE INDEX IF NOT EXISTS ""IX_demands_StoreId"" ON public.demands (""StoreId"");
                CREATE INDEX IF NOT EXISTS ""IX_demands_CurrencyId"" ON public.demands (""CurrencyId"");

                CREATE TABLE IF NOT EXISTS public.demand_lines (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""DemandId""         uuid NOT NULL,
                    ""ProductId""        uuid NOT NULL,
                    ""Quantity""         numeric(18,4) NOT NULL,
                    ""UnitPrice""        numeric(18,4) NOT NULL,
                    ""Discount""         numeric(18,4) NOT NULL,
                    ""LineTotal""        numeric(18,4) NOT NULL,
                    ""VatPercent""       numeric(5,2) NOT NULL,
                    ""SortOrder""        integer NOT NULL,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_demand_lines_demands_DemandId"" FOREIGN KEY (""DemandId"") REFERENCES public.demands(""Id"") ON DELETE CASCADE,
                    CONSTRAINT ""FK_demand_lines_products_ProductId"" FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT
                );

                CREATE INDEX IF NOT EXISTS ""IX_demand_lines_DemandId"" ON public.demand_lines (""DemandId"");
                CREATE INDEX IF NOT EXISTS ""IX_demand_lines_ProductId"" ON public.demand_lines (""ProductId"");
                CREATE INDEX IF NOT EXISTS ""IX_demand_lines_OrganizationId_ProductId"" ON public.demand_lines (""OrganizationId"", ""ProductId"");
            ");
        }

        protected override void Down(MigrationBuilder b)
        {
            b.Sql(@"
                DROP TABLE IF EXISTS public.demand_lines;
                DROP TABLE IF EXISTS public.demands;
            ");
        }
    }
}
