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

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations.LogsDb
{
    /// <inheritdoc />
    public partial class AddMerchantManagementLogs : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<string>(
                name: "Title",
                table: "merchant_audit_logs",
                type: "character varying(200)",
                maxLength: 200,
                nullable: false,
                comment: "标题。",
                oldClrType: typeof(string),
                oldType: "character varying(128)",
                oldMaxLength: 128,
                oldComment: "标题。");

            migrationBuilder.AlterColumn<string>(
                name: "OperatorName",
                table: "merchant_audit_logs",
                type: "character varying(100)",
                maxLength: 100,
                nullable: true,
                comment: "操作人名称。",
                oldClrType: typeof(string),
                oldType: "character varying(64)",
                oldMaxLength: 64,
                oldNullable: true,
                oldComment: "操作人名称。");

            migrationBuilder.AddColumn<string>(
                name: "IpAddress",
                table: "merchant_audit_logs",
                type: "character varying(50)",
                maxLength: 50,
                nullable: true,
                comment: "操作 IP。");

            migrationBuilder.CreateTable(
                name: "merchant_change_logs",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    MerchantId = table.Column<long>(type: "bigint", nullable: false, comment: "商户标识。"),
                    FieldName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false, comment: "变更字段名。"),
                    OldValue = table.Column<string>(type: "text", nullable: true, comment: "变更前值。"),
                    NewValue = table.Column<string>(type: "text", nullable: true, comment: "变更后值。"),
                    ChangeType = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, comment: "变更类型。"),
                    ChangedBy = table.Column<long>(type: "bigint", nullable: true, comment: "变更人 ID。"),
                    ChangedByName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true, comment: "变更人名称。"),
                    ChangeReason = 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_merchant_change_logs", x => x.Id);
                },
                comment: "商户变更日志。");

            migrationBuilder.CreateIndex(
                name: "IX_merchant_audit_logs_MerchantId_CreatedAt",
                table: "merchant_audit_logs",
                columns: new[] { "MerchantId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_merchant_audit_logs_TenantId_CreatedAt",
                table: "merchant_audit_logs",
                columns: new[] { "TenantId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_merchant_change_logs_MerchantId_CreatedAt",
                table: "merchant_change_logs",
                columns: new[] { "MerchantId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_merchant_change_logs_TenantId_CreatedAt",
                table: "merchant_change_logs",
                columns: new[] { "TenantId", "CreatedAt" });
        }

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

            migrationBuilder.DropIndex(
                name: "IX_merchant_audit_logs_MerchantId_CreatedAt",
                table: "merchant_audit_logs");

            migrationBuilder.DropIndex(
                name: "IX_merchant_audit_logs_TenantId_CreatedAt",
                table: "merchant_audit_logs");

            migrationBuilder.DropColumn(
                name: "IpAddress",
                table: "merchant_audit_logs");

            migrationBuilder.AlterColumn<string>(
                name: "Title",
                table: "merchant_audit_logs",
                type: "character varying(128)",
                maxLength: 128,
                nullable: false,
                comment: "标题。",
                oldClrType: typeof(string),
                oldType: "character varying(200)",
                oldMaxLength: 200,
                oldComment: "标题。");

            migrationBuilder.AlterColumn<string>(
                name: "OperatorName",
                table: "merchant_audit_logs",
                type: "character varying(64)",
                maxLength: 64,
                nullable: true,
                comment: "操作人名称。",
                oldClrType: typeof(string),
                oldType: "character varying(100)",
                oldMaxLength: 100,
                oldNullable: true,
                oldComment: "操作人名称。");
        }
    }
}
