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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddProductSkuSaveJobsAndReplaceIndex : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "product_sku_save_jobs",
                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: "所属门店 ID。"),
                    ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "所属商品 ID。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "任务状态。"),
                    Mode = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false, comment: "任务模式（当前固定 replace）。"),
                    PayloadJson = table.Column<string>(type: "text", nullable: false, comment: "任务请求负载 JSON 快照。"),
                    ProgressTotal = table.Column<int>(type: "integer", nullable: false, comment: "总处理数。"),
                    ProgressProcessed = table.Column<int>(type: "integer", nullable: false, comment: "已处理数。"),
                    FailedCount = table.Column<int>(type: "integer", nullable: false, comment: "失败条数。"),
                    ErrorMessage = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true, comment: "失败摘要。"),
                    HangfireJobId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "Hangfire 任务 ID。"),
                    StartedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "开始执行时间（UTC）。"),
                    FinishedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "完成时间（UTC）。"),
                    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_sku_save_jobs", x => x.Id);
                },
                comment: "商品 SKU 异步保存任务。");

            migrationBuilder.CreateIndex(
                name: "IX_product_skus_TenantId_ProductId",
                table: "product_skus",
                columns: new[] { "TenantId", "ProductId" });

            migrationBuilder.CreateIndex(
                name: "IX_product_sku_save_jobs_TenantId_ProductId_CreatedAt",
                table: "product_sku_save_jobs",
                columns: new[] { "TenantId", "ProductId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_product_sku_save_jobs_TenantId_Status_CreatedAt",
                table: "product_sku_save_jobs",
                columns: new[] { "TenantId", "Status", "CreatedAt" });
        }

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

            migrationBuilder.DropIndex(
                name: "IX_product_skus_TenantId_ProductId",
                table: "product_skus");
        }
    }
}
