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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddTenantQuotaUsageHistories : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "tenant_quota_usage_histories",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    QuotaType = table.Column<int>(type: "integer", nullable: false, comment: "配额类型。"),
                    UsedValue = table.Column<decimal>(type: "numeric", nullable: false, comment: "已使用值（记录时刻的快照）。"),
                    LimitValue = table.Column<decimal>(type: "numeric", nullable: false, comment: "限额值（记录时刻的快照）。"),
                    RecordedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "记录时间（UTC）。"),
                    ChangeType = table.Column<int>(type: "integer", nullable: false, comment: "变更类型。"),
                    ChangeAmount = table.Column<decimal>(type: "numeric", nullable: true, comment: "变更量（可选）。"),
                    ChangeReason = table.Column<string>(type: "character varying(256)", maxLength: 256, 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_usage_histories", x => x.Id);
                },
                comment: "租户配额使用历史记录（用于追踪配额上下限与使用量的时间序列变化）。");

            migrationBuilder.CreateIndex(
                name: "IX_tenant_quota_usage_histories_TenantId_QuotaType_RecordedAt",
                table: "tenant_quota_usage_histories",
                columns: new[] { "TenantId", "QuotaType", "RecordedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_tenant_quota_usage_histories_TenantId_RecordedAt",
                table: "tenant_quota_usage_histories",
                columns: new[] { "TenantId", "RecordedAt" });
        }

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