using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ColaFlow.Modules.Mcp.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddPendingChangeAndTaskLock : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "pending_changes",
                schema: "mcp",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    api_key_id = table.Column<Guid>(type: "uuid", nullable: false),
                    tool_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    diff = table.Column<string>(type: "jsonb", nullable: false),
                    status = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    created_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    expires_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    approved_by = table.Column<Guid>(type: "uuid", nullable: true),
                    approved_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    rejected_by = table.Column<Guid>(type: "uuid", nullable: true),
                    rejected_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    rejection_reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    applied_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    application_result = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_pending_changes", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "task_locks",
                schema: "mcp",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    resource_type = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    resource_id = table.Column<Guid>(type: "uuid", nullable: false),
                    lock_holder_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    lock_holder_id = table.Column<Guid>(type: "uuid", nullable: false),
                    lock_holder_name = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    status = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    acquired_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    expires_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    released_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    purpose = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_task_locks", x => x.id);
                });

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_api_key_id",
                schema: "mcp",
                table: "pending_changes",
                column: "api_key_id");

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_created_at",
                schema: "mcp",
                table: "pending_changes",
                column: "created_at");

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_expires_at",
                schema: "mcp",
                table: "pending_changes",
                column: "expires_at");

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_status",
                schema: "mcp",
                table: "pending_changes",
                column: "status");

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_tenant_id",
                schema: "mcp",
                table: "pending_changes",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "ix_pending_changes_tenant_status",
                schema: "mcp",
                table: "pending_changes",
                columns: new[] { "tenant_id", "status" });

            migrationBuilder.CreateIndex(
                name: "ix_task_locks_expires_at",
                schema: "mcp",
                table: "task_locks",
                column: "expires_at");

            migrationBuilder.CreateIndex(
                name: "ix_task_locks_lock_holder_id",
                schema: "mcp",
                table: "task_locks",
                column: "lock_holder_id");

            migrationBuilder.CreateIndex(
                name: "ix_task_locks_resource",
                schema: "mcp",
                table: "task_locks",
                columns: new[] { "tenant_id", "resource_type", "resource_id" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_task_locks_status",
                schema: "mcp",
                table: "task_locks",
                column: "status");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "pending_changes",
                schema: "mcp");

            migrationBuilder.DropTable(
                name: "task_locks",
                schema: "mcp");
        }
    }
}
