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

#nullable disable

namespace Nexus.Api.Migrations
{
    /// <inheritdoc />
    public partial class AddOpenClawRunProjection : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "OpenClawRuns",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Title = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: false),
                    Prompt = table.Column<string>(type: "text", nullable: false),
                    AgentId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    SessionKey = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    Status = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    TaskId = table.Column<Guid>(type: "uuid", nullable: true),
                    ProjectId = table.Column<Guid>(type: "uuid", nullable: true),
                    OpenClawRunId = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: true),
                    RetriedFromRunId = table.Column<Guid>(type: "uuid", nullable: true),
                    StartIdempotencyKey = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    CorrelationId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Actor = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: false),
                    TraceParent = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    LastError = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    LastGatewaySequence = table.Column<long>(type: "bigint", nullable: true),
                    SequenceGapDetected = table.Column<bool>(type: "boolean", nullable: false),
                    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),
                    StartedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    FinishedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OpenClawRuns", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OpenClawRuns_Projects_ProjectId",
                        column: x => x.ProjectId,
                        principalTable: "Projects",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_OpenClawRuns_Tasks_TaskId",
                        column: x => x.TaskId,
                        principalTable: "Tasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "OpenClawRunHistory",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    RunId = table.Column<Guid>(type: "uuid", nullable: false),
                    Action = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    FromStatus = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    ToStatus = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    Message = table.Column<string>(type: "character varying(2000)", maxLength: 2000, 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),
                    IdempotencyKey = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    TraceParent = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
                    GatewayEventId = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    GatewaySequence = table.Column<long>(type: "bigint", nullable: true),
                    SequenceGapDetected = table.Column<bool>(type: "boolean", nullable: false),
                    ResultRunId = table.Column<Guid>(type: "uuid", nullable: true),
                    OccurredAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OpenClawRunHistory", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OpenClawRunHistory_OpenClawRuns_RunId",
                        column: x => x.RunId,
                        principalTable: "OpenClawRuns",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRunHistory_GatewayEventId",
                table: "OpenClawRunHistory",
                column: "GatewayEventId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRunHistory_RunId_Action_IdempotencyKey",
                table: "OpenClawRunHistory",
                columns: new[] { "RunId", "Action", "IdempotencyKey" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRunHistory_RunId_OccurredAt",
                table: "OpenClawRunHistory",
                columns: new[] { "RunId", "OccurredAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_OpenClawRunId",
                table: "OpenClawRuns",
                column: "OpenClawRunId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_ProjectId",
                table: "OpenClawRuns",
                column: "ProjectId");

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_RetriedFromRunId",
                table: "OpenClawRuns",
                column: "RetriedFromRunId");

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_SessionKey",
                table: "OpenClawRuns",
                column: "SessionKey");

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_StartIdempotencyKey",
                table: "OpenClawRuns",
                column: "StartIdempotencyKey",
                unique: true);

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

            migrationBuilder.CreateIndex(
                name: "IX_OpenClawRuns_TaskId",
                table: "OpenClawRuns",
                column: "TaskId");
        }

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

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