using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddProductSpecTemplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "product_spec_template_options",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    TemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "模板 ID。"),
                    Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "选项名称。"),
                    ExtraPrice = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "附加价格。"),
                    SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_product_spec_template_options", x => x.Id);
                },
                comment: "规格做法模板选项。");

            migrationBuilder.CreateTable(
                name: "product_spec_template_products",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
                    TemplateId = table.Column<long>(type: "bigint", nullable: false, comment: "模板 ID。"),
                    ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "商品 ID。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_product_spec_template_products", x => x.Id);
                },
                comment: "规格做法模板与商品关联。");

            migrationBuilder.CreateTable(
                name: "product_spec_templates",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
                    Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "模板名称。"),
                    TemplateType = table.Column<int>(type: "integer", nullable: false, comment: "模板类型。"),
                    SelectionType = table.Column<int>(type: "integer", nullable: false, comment: "选择方式。"),
                    IsRequired = table.Column<bool>(type: "boolean", nullable: false, comment: "是否必选。"),
                    SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, comment: "是否启用。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_product_spec_templates", x => x.Id);
                },
                comment: "门店规格做法模板。");

            migrationBuilder.CreateIndex(
                name: "IX_product_spec_template_options_TenantId_TemplateId_Name",
                table: "product_spec_template_options",
                columns: new[] { "TenantId", "TemplateId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_product_spec_template_products_TenantId_StoreId_ProductId",
                table: "product_spec_template_products",
                columns: new[] { "TenantId", "StoreId", "ProductId" });

            migrationBuilder.CreateIndex(
                name: "IX_product_spec_template_products_TenantId_StoreId_TemplateId_~",
                table: "product_spec_template_products",
                columns: new[] { "TenantId", "StoreId", "TemplateId", "ProductId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_product_spec_templates_TenantId_StoreId_Name",
                table: "product_spec_templates",
                columns: new[] { "TenantId", "StoreId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_product_spec_templates_TenantId_StoreId_TemplateType_IsEnab~",
                table: "product_spec_templates",
                columns: new[] { "TenantId", "StoreId", "TemplateType", "IsEnabled" });
        }

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

            migrationBuilder.DropTable(
                name: "product_spec_template_products");

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