using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>SuperAdmin console: organizations.IsArchived/ArchivedAt/
    /// AccountOwnerUserId + новая таблица super_admin_audit_log для журнала
    /// действий супер-админа (создание/правка/архивирование орг,
    /// смена аккаунт-владельца, правки в режиме «войти как»).</summary>
    public partial class Phase4_SuperAdminConsole : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.AddColumn<bool>(
                name: "IsArchived", schema: "public", table: "organizations",
                type: "boolean", nullable: false, defaultValue: false);
            b.AddColumn<System.DateTime>(
                name: "ArchivedAt", schema: "public", table: "organizations",
                type: "timestamp with time zone", nullable: true);
            b.AddColumn<System.Guid>(
                name: "AccountOwnerUserId", schema: "public", table: "organizations",
                type: "uuid", nullable: true);
            b.CreateIndex(
                name: "IX_organizations_IsArchived",
                schema: "public",
                table: "organizations",
                column: "IsArchived");

            b.CreateTable(
                name: "super_admin_audit_log",
                schema: "public",
                columns: table => new
                {
                    Id = table.Column<System.Guid>(type: "uuid", nullable: false),
                    SuperAdminUserId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    ActionType = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    OrganizationId = table.Column<System.Guid>(type: "uuid", nullable: true),
                    EntityType = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    EntityId = table.Column<System.Guid>(type: "uuid", nullable: true),
                    Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    Reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    ChangesJson = table.Column<string>(type: "jsonb", nullable: false),
                    IpAddress = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: false),
                    CreatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: true),
                },
                constraints: table => table.PrimaryKey("PK_super_admin_audit_log", x => x.Id));
            b.CreateIndex(
                name: "IX_super_admin_audit_log_CreatedAt",
                schema: "public", table: "super_admin_audit_log", column: "CreatedAt");
            b.CreateIndex(
                name: "IX_super_admin_audit_log_SuperAdminUserId_CreatedAt",
                schema: "public", table: "super_admin_audit_log",
                columns: new[] { "SuperAdminUserId", "CreatedAt" });
            b.CreateIndex(
                name: "IX_super_admin_audit_log_OrganizationId_CreatedAt",
                schema: "public", table: "super_admin_audit_log",
                columns: new[] { "OrganizationId", "CreatedAt" });
        }

        protected override void Down(MigrationBuilder b)
        {
            b.DropTable(name: "super_admin_audit_log", schema: "public");
            b.DropIndex(name: "IX_organizations_IsArchived", schema: "public", table: "organizations");
            b.DropColumn(name: "AccountOwnerUserId", schema: "public", table: "organizations");
            b.DropColumn(name: "ArchivedAt", schema: "public", table: "organizations");
            b.DropColumn(name: "IsArchived", schema: "public", table: "organizations");
        }
    }
}
