using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CartWise.Infrastructure.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddProductCatalog : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Brands",
                columns: table => new
                {
                    BrandId = table.Column<Guid>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 160, nullable: false),
                    NormalizedName = table.Column<string>(type: "TEXT", maxLength: 160, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Brands", x => x.BrandId);
                });

            migrationBuilder.CreateTable(
                name: "Products",
                columns: table => new
                {
                    ProductId = table.Column<Guid>(type: "TEXT", nullable: false),
                    GroceryConceptId = table.Column<Guid>(type: "TEXT", nullable: true),
                    BrandId = table.Column<Guid>(type: "TEXT", nullable: true),
                    Name = table.Column<string>(type: "TEXT", maxLength: 240, nullable: false),
                    NormalizedName = table.Column<string>(type: "TEXT", maxLength: 240, nullable: false),
                    SizeValue = table.Column<decimal>(type: "TEXT", nullable: true),
                    SizeUnit = table.Column<string>(type: "TEXT", maxLength: 32, nullable: true),
                    ImageUrl = table.Column<string>(type: "TEXT", nullable: true),
                    SourceType = table.Column<int>(type: "INTEGER", nullable: false),
                    SourceExternalId = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
                    CreatedUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Products", x => x.ProductId);
                    table.ForeignKey(
                        name: "FK_Products_Brands_BrandId",
                        column: x => x.BrandId,
                        principalTable: "Brands",
                        principalColumn: "BrandId",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_Products_GroceryConcepts_GroceryConceptId",
                        column: x => x.GroceryConceptId,
                        principalTable: "GroceryConcepts",
                        principalColumn: "GroceryConceptId",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "HouseholdProductPreferences",
                columns: table => new
                {
                    HouseholdProductPreferenceId = table.Column<Guid>(type: "TEXT", nullable: false),
                    HouseholdId = table.Column<Guid>(type: "TEXT", nullable: false),
                    GroceryConceptId = table.Column<Guid>(type: "TEXT", nullable: false),
                    PreferredProductId = table.Column<Guid>(type: "TEXT", nullable: true),
                    SubstitutionLevel = table.Column<int>(type: "INTEGER", nullable: false),
                    PreferredQuantity = table.Column<decimal>(type: "TEXT", nullable: true),
                    PreferredUnit = table.Column<string>(type: "TEXT", maxLength: 32, nullable: true),
                    UpdatedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_HouseholdProductPreferences", x => x.HouseholdProductPreferenceId);
                    table.ForeignKey(
                        name: "FK_HouseholdProductPreferences_GroceryConcepts_GroceryConceptId",
                        column: x => x.GroceryConceptId,
                        principalTable: "GroceryConcepts",
                        principalColumn: "GroceryConceptId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_HouseholdProductPreferences_Households_HouseholdId",
                        column: x => x.HouseholdId,
                        principalTable: "Households",
                        principalColumn: "HouseholdId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_HouseholdProductPreferences_Products_PreferredProductId",
                        column: x => x.PreferredProductId,
                        principalTable: "Products",
                        principalColumn: "ProductId",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "ProductIdentifiers",
                columns: table => new
                {
                    ProductIdentifierId = table.Column<Guid>(type: "TEXT", nullable: false),
                    ProductId = table.Column<Guid>(type: "TEXT", nullable: false),
                    IdentifierType = table.Column<int>(type: "INTEGER", nullable: false),
                    Value = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    NormalizedValue = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProductIdentifiers", x => x.ProductIdentifierId);
                    table.ForeignKey(
                        name: "FK_ProductIdentifiers_Products_ProductId",
                        column: x => x.ProductId,
                        principalTable: "Products",
                        principalColumn: "ProductId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Brands_NormalizedName",
                table: "Brands",
                column: "NormalizedName",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_HouseholdProductPreferences_GroceryConceptId",
                table: "HouseholdProductPreferences",
                column: "GroceryConceptId");

            migrationBuilder.CreateIndex(
                name: "IX_HouseholdProductPreferences_HouseholdId_GroceryConceptId",
                table: "HouseholdProductPreferences",
                columns: new[] { "HouseholdId", "GroceryConceptId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_HouseholdProductPreferences_PreferredProductId",
                table: "HouseholdProductPreferences",
                column: "PreferredProductId");

            migrationBuilder.CreateIndex(
                name: "IX_ProductIdentifiers_IdentifierType_NormalizedValue",
                table: "ProductIdentifiers",
                columns: new[] { "IdentifierType", "NormalizedValue" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ProductIdentifiers_ProductId",
                table: "ProductIdentifiers",
                column: "ProductId");

            migrationBuilder.CreateIndex(
                name: "IX_Products_BrandId",
                table: "Products",
                column: "BrandId");

            migrationBuilder.CreateIndex(
                name: "IX_Products_GroceryConceptId",
                table: "Products",
                column: "GroceryConceptId");

            migrationBuilder.CreateIndex(
                name: "IX_Products_NormalizedName",
                table: "Products",
                column: "NormalizedName");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "HouseholdProductPreferences");

            migrationBuilder.DropTable(
                name: "ProductIdentifiers");

            migrationBuilder.DropTable(
                name: "Products");

            migrationBuilder.DropTable(
                name: "Brands");
        }
    }
}
