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

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Phase6f — возврат поставщику (SupplierReturn).
    ///
    /// Документ возврата товара поставщику. При проведении создаёт
    /// stock_movements тип SupplierReturn с отрицательным Quantity.
    /// Опционально ссылается на исходную приёмку.</summary>
    [DbContext(typeof(AppDbContext))]
    [Migration("20260528050000_Phase6f_SupplierReturns")]
    public partial class Phase6f_SupplierReturns : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.Sql(@"
                CREATE TABLE IF NOT EXISTS public.supplier_returns (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""Number""           varchar(50) NOT NULL,
                    ""Date""             timestamp with time zone NOT NULL,
                    ""Status""           integer NOT NULL,
                    ""SupplierId""       uuid NOT NULL,
                    ""StoreId""          uuid NOT NULL,
                    ""CurrencyId""       uuid NOT NULL,
                    ""ReferenceSupplyId"" uuid,
                    ""Notes""            varchar(1000),
                    ""Total""            numeric(18,4) NOT NULL,
                    ""PostedAt""         timestamp with time zone,
                    ""PostedByUserId""   uuid,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_supplier_returns_counterparties_SupplierId"" FOREIGN KEY (""SupplierId"") REFERENCES public.counterparties(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_supplier_returns_stores_StoreId"" FOREIGN KEY (""StoreId"") REFERENCES public.stores(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_supplier_returns_currencies_CurrencyId"" FOREIGN KEY (""CurrencyId"") REFERENCES public.currencies(""Id"") ON DELETE RESTRICT,
                    CONSTRAINT ""FK_supplier_returns_supplies_ReferenceSupplyId"" FOREIGN KEY (""ReferenceSupplyId"") REFERENCES public.supplies(""Id"") ON DELETE RESTRICT
                );

                CREATE UNIQUE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Number"" ON public.supplier_returns (""OrganizationId"", ""Number"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Date"" ON public.supplier_returns (""OrganizationId"", ""Date"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_Status"" ON public.supplier_returns (""OrganizationId"", ""Status"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_OrganizationId_SupplierId"" ON public.supplier_returns (""OrganizationId"", ""SupplierId"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_StoreId"" ON public.supplier_returns (""StoreId"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_CurrencyId"" ON public.supplier_returns (""CurrencyId"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_returns_ReferenceSupplyId"" ON public.supplier_returns (""ReferenceSupplyId"");

                CREATE TABLE IF NOT EXISTS public.supplier_return_lines (
                    ""Id""               uuid PRIMARY KEY,
                    ""OrganizationId""   uuid NOT NULL,
                    ""SupplierReturnId"" uuid NOT NULL,
                    ""ProductId""        uuid NOT NULL,
                    ""Quantity""         numeric(18,4) NOT NULL,
                    ""UnitPrice""        numeric(18,4) NOT NULL,
                    ""LineTotal""        numeric(18,4) NOT NULL,
                    ""SortOrder""        integer NOT NULL,
                    ""CreatedAt""        timestamp with time zone NOT NULL,
                    ""UpdatedAt""        timestamp with time zone,
                    CONSTRAINT ""FK_supplier_return_lines_supplier_returns_SupplierReturnId""
                        FOREIGN KEY (""SupplierReturnId"") REFERENCES public.supplier_returns(""Id"") ON DELETE CASCADE,
                    CONSTRAINT ""FK_supplier_return_lines_products_ProductId""
                        FOREIGN KEY (""ProductId"") REFERENCES public.products(""Id"") ON DELETE RESTRICT
                );

                CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_SupplierReturnId"" ON public.supplier_return_lines (""SupplierReturnId"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_ProductId"" ON public.supplier_return_lines (""ProductId"");
                CREATE INDEX IF NOT EXISTS ""IX_supplier_return_lines_OrganizationId_ProductId"" ON public.supplier_return_lines (""OrganizationId"", ""ProductId"");
            ");
        }

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