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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddQuotaPackagesAndPayments : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "operation_logs",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    OperationType = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "操作类型：BatchExtend, BatchRemind, StatusChange 等。"),
                    TargetType = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "目标类型：Subscription, Bill 等。"),
                    TargetIds = table.Column<string>(type: "text", nullable: true, comment: "目标ID列表（JSON）。"),
                    OperatorId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "操作人ID。"),
                    OperatorName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true, comment: "操作人名称。"),
                    Parameters = table.Column<string>(type: "text", nullable: true, comment: "操作参数（JSON）。"),
                    Result = table.Column<string>(type: "text", nullable: true, comment: "操作结果（JSON）。"),
                    Success = 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。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_operation_logs", x => x.Id);
                },
                comment: "运营操作日志。");

            migrationBuilder.CreateTable(
                name: "quota_packages",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "配额包名称。"),
                    QuotaType = table.Column<int>(type: "integer", nullable: false, comment: "配额类型。"),
                    QuotaValue = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "配额数值。"),
                    Price = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "价格。"),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false, comment: "是否上架。"),
                    SortOrder = table.Column<int>(type: "integer", nullable: false, defaultValue: 0, comment: "排序。"),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, 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。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_quota_packages", x => x.Id);
                },
                comment: "配额包定义（平台提供的可购买配额包）。");

            migrationBuilder.CreateTable(
                name: "tenant_payments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    BillingStatementId = table.Column<long>(type: "bigint", nullable: false, comment: "关联的账单 ID。"),
                    Amount = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "支付金额。"),
                    Method = table.Column<int>(type: "integer", nullable: false, comment: "支付方式。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "支付状态。"),
                    TransactionNo = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "交易号。"),
                    ProofUrl = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "支付凭证 URL。"),
                    PaidAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "支付时间。"),
                    Notes = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, 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_tenant_payments", x => x.Id);
                },
                comment: "租户支付记录。");

            migrationBuilder.CreateTable(
                name: "tenant_quota_package_purchases",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    QuotaPackageId = table.Column<long>(type: "bigint", nullable: false, comment: "配额包 ID。"),
                    QuotaValue = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "购买时的配额值。"),
                    Price = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false, comment: "购买价格。"),
                    PurchasedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "购买时间。"),
                    ExpiredAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "过期时间（可选）。"),
                    Notes = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, 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_tenant_quota_package_purchases", x => x.Id);
                },
                comment: "租户配额包购买记录。");

            migrationBuilder.CreateIndex(
                name: "IX_operation_logs_CreatedAt",
                table: "operation_logs",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_operation_logs_OperationType_CreatedAt",
                table: "operation_logs",
                columns: new[] { "OperationType", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_quota_packages_QuotaType_IsActive_SortOrder",
                table: "quota_packages",
                columns: new[] { "QuotaType", "IsActive", "SortOrder" });

            migrationBuilder.CreateIndex(
                name: "IX_tenant_payments_TenantId_BillingStatementId",
                table: "tenant_payments",
                columns: new[] { "TenantId", "BillingStatementId" });

            migrationBuilder.CreateIndex(
                name: "IX_tenant_quota_package_purchases_TenantId_QuotaPackageId_Purc~",
                table: "tenant_quota_package_purchases",
                columns: new[] { "TenantId", "QuotaPackageId", "PurchasedAt" });
        }

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

            migrationBuilder.DropTable(
                name: "quota_packages");

            migrationBuilder.DropTable(
                name: "tenant_payments");

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