using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddPlatformGovernance : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "platform_approval_policies",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    code = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    required_permission = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    enabled = table.Column<bool>(type: "boolean", nullable: false),
                    always_require_approval = table.Column<bool>(type: "boolean", nullable: false),
                    amount_threshold_cents = table.Column<int>(type: "integer", nullable: true),
                    version = table.Column<int>(type: "integer", nullable: false),
                    expires_after_hours = table.Column<int>(type: "integer", nullable: false),
                    conditions = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_platform_approval_policies", x => x.id);
                    table.CheckConstraint("ck_platform_approval_policies_expiry", "expires_after_hours between 1 and 720");
                    table.CheckConstraint("ck_platform_approval_policies_threshold", "amount_threshold_cents is null or amount_threshold_cents > 0");
                    table.CheckConstraint("ck_platform_approval_policies_version", "version > 0");
                });

            migrationBuilder.CreateTable(
                name: "platform_approval_requests",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    request_no = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    policy_code = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    policy_version = table.Column<int>(type: "integer", nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    requested_by = table.Column<Guid>(type: "uuid", nullable: false),
                    decided_by = table.Column<Guid>(type: "uuid", nullable: true),
                    required_permission = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    command_type = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    target_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    target_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    amount_cents = table.Column<int>(type: "integer", nullable: true),
                    idempotency_key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    request_hash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    request_snapshot = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    result_snapshot = table.Column<JsonElement>(type: "jsonb", nullable: true),
                    request_reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    decision_reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    error = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    expires_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    decided_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    executed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    concurrency_stamp = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_platform_approval_requests", x => x.id);
                    table.ForeignKey(
                        name: "fk_platform_approval_requests_users_decided_by",
                        column: x => x.decided_by,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "fk_platform_approval_requests_users_requested_by",
                        column: x => x.requested_by,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "ix_platform_approval_policies_code",
                table: "platform_approval_policies",
                column: "code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_approval_requests_decided_by",
                table: "platform_approval_requests",
                column: "decided_by");

            migrationBuilder.CreateIndex(
                name: "ix_platform_approval_requests_request_no",
                table: "platform_approval_requests",
                column: "request_no",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_approval_requests_requested_by_command_type_idempo~",
                table: "platform_approval_requests",
                columns: new[] { "requested_by", "command_type", "idempotency_key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_approval_requests_status_expires_at_created_at",
                table: "platform_approval_requests",
                columns: new[] { "status", "expires_at", "created_at" });
        }

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

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