﻿BEGIN TRANSACTION;
ALTER TABLE "Products" ADD "VariantCollectionId" TEXT NULL;

ALTER TABLE "Products" ADD "VariantsJson" TEXT NULL;

ALTER TABLE "OrderItems" ADD "SelectedVariants" TEXT NULL;

CREATE TABLE "SalesLedgers" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_SalesLedgers" PRIMARY KEY,
    "OrderId" TEXT NOT NULL,
    "ProductId" TEXT NOT NULL,
    "ProductName" TEXT NOT NULL,
    "Quantity" INTEGER NOT NULL,
    "SalePriceFiat" decimal(18,2) NOT NULL,
    "FiatCurrency" TEXT NOT NULL,
    "SalePriceBTC" decimal(18,8) NULL,
    "Cryptocurrency" TEXT NULL,
    "SoldAt" TEXT NOT NULL,
    CONSTRAINT "FK_SalesLedgers_Orders_OrderId" FOREIGN KEY ("OrderId") REFERENCES "Orders" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_SalesLedgers_Products_ProductId" FOREIGN KEY ("ProductId") REFERENCES "Products" ("Id") ON DELETE RESTRICT
);

CREATE TABLE "VariantCollections" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_VariantCollections" PRIMARY KEY,
    "Name" TEXT NOT NULL,
    "PropertiesJson" TEXT NOT NULL,
    "IsActive" INTEGER NOT NULL,
    "CreatedAt" TEXT NOT NULL,
    "UpdatedAt" TEXT NOT NULL
);

CREATE INDEX "IX_Products_VariantCollectionId" ON "Products" ("VariantCollectionId");

CREATE INDEX "IX_SalesLedgers_OrderId" ON "SalesLedgers" ("OrderId");

CREATE INDEX "IX_SalesLedgers_ProductId" ON "SalesLedgers" ("ProductId");

CREATE INDEX "IX_SalesLedgers_ProductId_SoldAt" ON "SalesLedgers" ("ProductId", "SoldAt");

CREATE INDEX "IX_SalesLedgers_SoldAt" ON "SalesLedgers" ("SoldAt");

CREATE INDEX "IX_VariantCollections_IsActive" ON "VariantCollections" ("IsActive");

CREATE INDEX "IX_VariantCollections_Name" ON "VariantCollections" ("Name");

CREATE TABLE "ef_temp_OrderItems" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_OrderItems" PRIMARY KEY,
    "OrderId" TEXT NOT NULL,
    "ProductId" TEXT NOT NULL,
    "ProductMultiBuyId" TEXT NULL,
    "Quantity" INTEGER NOT NULL,
    "SelectedVariants" TEXT NULL,
    "TotalPrice" decimal(18,2) NOT NULL,
    "UnitPrice" decimal(18,2) NOT NULL,
    CONSTRAINT "FK_OrderItems_Orders_OrderId" FOREIGN KEY ("OrderId") REFERENCES "Orders" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_OrderItems_ProductMultiBuys_ProductMultiBuyId" FOREIGN KEY ("ProductMultiBuyId") REFERENCES "ProductMultiBuys" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_OrderItems_Products_ProductId" FOREIGN KEY ("ProductId") REFERENCES "Products" ("Id") ON DELETE RESTRICT
);

INSERT INTO "ef_temp_OrderItems" ("Id", "OrderId", "ProductId", "ProductMultiBuyId", "Quantity", "SelectedVariants", "TotalPrice", "UnitPrice")
SELECT "Id", "OrderId", "ProductId", "ProductMultiBuyId", "Quantity", "SelectedVariants", "TotalPrice", "UnitPrice"
FROM "OrderItems";

CREATE TABLE "ef_temp_Products" (
    "Id" TEXT NOT NULL CONSTRAINT "PK_Products" PRIMARY KEY,
    "CategoryId" TEXT NOT NULL,
    "CreatedAt" TEXT NOT NULL,
    "Description" TEXT NOT NULL,
    "IsActive" INTEGER NOT NULL,
    "Name" TEXT NOT NULL,
    "Price" decimal(18,2) NOT NULL,
    "StockQuantity" INTEGER NOT NULL,
    "UpdatedAt" TEXT NOT NULL,
    "VariantCollectionId" TEXT NULL,
    "VariantsJson" TEXT NULL,
    "Weight" decimal(18,4) NOT NULL,
    "WeightUnit" INTEGER NOT NULL,
    CONSTRAINT "FK_Products_Categories_CategoryId" FOREIGN KEY ("CategoryId") REFERENCES "Categories" ("Id") ON DELETE RESTRICT,
    CONSTRAINT "FK_Products_VariantCollections_VariantCollectionId" FOREIGN KEY ("VariantCollectionId") REFERENCES "VariantCollections" ("Id")
);

INSERT INTO "ef_temp_Products" ("Id", "CategoryId", "CreatedAt", "Description", "IsActive", "Name", "Price", "StockQuantity", "UpdatedAt", "VariantCollectionId", "VariantsJson", "Weight", "WeightUnit")
SELECT "Id", "CategoryId", "CreatedAt", "Description", "IsActive", "Name", "Price", "StockQuantity", "UpdatedAt", "VariantCollectionId", "VariantsJson", "Weight", "WeightUnit"
FROM "Products";

COMMIT;

PRAGMA foreign_keys = 0;

BEGIN TRANSACTION;
DROP TABLE "OrderItems";

ALTER TABLE "ef_temp_OrderItems" RENAME TO "OrderItems";

DROP TABLE "Products";

ALTER TABLE "ef_temp_Products" RENAME TO "Products";

COMMIT;

PRAGMA foreign_keys = 1;

BEGIN TRANSACTION;
CREATE INDEX "IX_OrderItems_OrderId" ON "OrderItems" ("OrderId");

CREATE INDEX "IX_OrderItems_ProductId" ON "OrderItems" ("ProductId");

CREATE INDEX "IX_OrderItems_ProductMultiBuyId" ON "OrderItems" ("ProductMultiBuyId");

CREATE INDEX "IX_Products_CategoryId" ON "Products" ("CategoryId");

CREATE INDEX "IX_Products_VariantCollectionId" ON "Products" ("VariantCollectionId");

COMMIT;

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20250928014546_AddVariantCollectionsAndSalesLedger', '9.0.9');

