using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Nexus.Api.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Nexus.Api.Migrations
{
    /// <inheritdoc />
    [DbContext(typeof(NexusDbContext))]
    [Migration("20260730224500_AddAgentProvisioningAndBoardIndexes")]
    public partial class AddAgentProvisioningAndBoardIndexes : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AgentProposals",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Source = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    RequestedName = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    RequestedAgentId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    Role = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: true),
                    Description = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    Model = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: true),
                    Emoji = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
                    Avatar = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
                    Workspace = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: false),
                    StandardFilesJson = table.Column<string>(type: "jsonb", nullable: false),
                    StandardFilesHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    Status = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    RequestedBy = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: false),
                    ApprovedBy = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: true),
                    RejectedBy = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: true),
                    RejectionReason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    OpenClawAgentId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    OpenClawWorkspace = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
                    LastErrorCode = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    LastErrorMessage = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    Revision = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    ApprovedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    RejectedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    CompletedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AgentProposals", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OperationClaims",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Operation = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    IdempotencyKeyHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    RequestHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    ResourceId = table.Column<Guid>(type: "uuid", nullable: true),
                    State = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    ResultCode = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    CompletedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    ExpiresAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OperationClaims", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OutboxEvents",
                columns: table => new
                {
                    Sequence = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    EventId = table.Column<Guid>(type: "uuid", nullable: false),
                    Type = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    AggregateType = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    AggregateId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    AggregateRevision = table.Column<int>(type: "integer", nullable: false),
                    PayloadJson = table.Column<string>(type: "jsonb", nullable: false),
                    OccurredAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    PublishedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    PublishAttempts = table.Column<int>(type: "integer", nullable: false),
                    LastErrorCode = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OutboxEvents", x => x.Sequence);
                });

            migrationBuilder.CreateTable(
                name: "AgentProvisionRequests",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ProposalId = table.Column<Guid>(type: "uuid", nullable: false),
                    Attempt = table.Column<int>(type: "integer", nullable: false),
                    Stage = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    Status = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    IdempotencyKeyHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    Actor = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: false),
                    CorrelationId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    TraceParent = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    OpenClawAgentId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    LastErrorCode = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    LastErrorMessage = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    LeaseOwner = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: true),
                    LeaseUntil = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    Revision = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    DispatchStartedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    CompletedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AgentProvisionRequests", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AgentProvisionRequests_AgentProposals_ProposalId",
                        column: x => x.ProposalId,
                        principalTable: "AgentProposals",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Activity_TaskId_CreatedAt_Id",
                table: "Activity",
                columns: new[] { "TaskId", "CreatedAt", "Id" },
                descending: new[] { false, true, true },
                filter: "\"TaskId\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_AgentProposals_OpenClawAgentId",
                table: "AgentProposals",
                column: "OpenClawAgentId");

            migrationBuilder.CreateIndex(
                name: "IX_AgentProposals_RequestedAgentId",
                table: "AgentProposals",
                column: "RequestedAgentId");

            migrationBuilder.CreateIndex(
                name: "IX_AgentProposals_Status_UpdatedAt",
                table: "AgentProposals",
                columns: new[] { "Status", "UpdatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AgentProvisionRequests_LeaseUntil",
                table: "AgentProvisionRequests",
                column: "LeaseUntil");

            migrationBuilder.CreateIndex(
                name: "IX_AgentProvisionRequests_ProposalId_Attempt",
                table: "AgentProvisionRequests",
                columns: new[] { "ProposalId", "Attempt" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_AgentProvisionRequests_Status_CreatedAt",
                table: "AgentProvisionRequests",
                columns: new[] { "Status", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OperationClaims_ExpiresAt",
                table: "OperationClaims",
                column: "ExpiresAt");

            migrationBuilder.CreateIndex(
                name: "IX_OperationClaims_Operation_IdempotencyKeyHash",
                table: "OperationClaims",
                columns: new[] { "Operation", "IdempotencyKeyHash" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OutboxEvents_EventId",
                table: "OutboxEvents",
                column: "EventId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OutboxEvents_OccurredAt",
                table: "OutboxEvents",
                column: "OccurredAt");

            migrationBuilder.CreateIndex(
                name: "IX_OutboxEvents_PublishedAt_Sequence",
                table: "OutboxEvents",
                columns: new[] { "PublishedAt", "Sequence" });

            migrationBuilder.CreateIndex(
                name: "IX_Tasks_Done_UpdatedAt_Id",
                table: "Tasks",
                columns: new[] { "UpdatedAt", "Id" },
                descending: new[] { true, true },
                filter: "\"State\" = 'Done'");

            migrationBuilder.CreateIndex(
                name: "IX_Tasks_AgentWorkflow",
                table: "Tasks",
                columns: new[] { "ExpectedFrom", "State", "UpdatedAt", "Id" },
                descending: new[] { false, false, true, false },
                filter: "\"IsAgentTask\" = TRUE");

            migrationBuilder.CreateIndex(
                name: "IX_Tasks_ParentTaskId_State",
                table: "Tasks",
                columns: new[] { "ParentTaskId", "State" },
                filter: "\"ParentTaskId\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_Tasks_State_UpdatedAt_Id_Board",
                table: "Tasks",
                columns: new[] { "State", "UpdatedAt", "Id" },
                descending: new[] { false, true, false });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_Activity_TaskId_CreatedAt_Id",
                table: "Activity");

            migrationBuilder.DropIndex(
                name: "IX_Tasks_Done_UpdatedAt_Id",
                table: "Tasks");

            migrationBuilder.DropIndex(
                name: "IX_Tasks_AgentWorkflow",
                table: "Tasks");

            migrationBuilder.DropIndex(
                name: "IX_Tasks_ParentTaskId_State",
                table: "Tasks");

            migrationBuilder.DropIndex(
                name: "IX_Tasks_State_UpdatedAt_Id_Board",
                table: "Tasks");

            migrationBuilder.DropTable(
                name: "AgentProvisionRequests");

            migrationBuilder.DropTable(
                name: "OperationClaims");

            migrationBuilder.DropTable(
                name: "OutboxEvents");

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