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

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class P0SystemStrengthening : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "cancellation_reason",
                table: "background_jobs",
                type: "character varying(1000)",
                maxLength: 1000,
                nullable: true);

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "cancellation_requested_at",
                table: "background_jobs",
                type: "timestamp with time zone",
                nullable: true);

            migrationBuilder.AddColumn<Guid>(
                name: "cancellation_requested_by",
                table: "background_jobs",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "idempotency_key",
                table: "background_jobs",
                type: "character varying(200)",
                maxLength: 200,
                nullable: true);

            migrationBuilder.CreateTable(
                name: "tenant_lifecycle_operations",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    operation_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    requested_by = table.Column<Guid>(type: "uuid", nullable: false),
                    target_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    export_asset_id = table.Column<Guid>(type: "uuid", nullable: true),
                    reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    last_error = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    result = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    tenant_id = 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_tenant_lifecycle_operations", x => x.id);
                    table.UniqueConstraint("ak_tenant_lifecycle_operations_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.ForeignKey(
                        name: "fk_tenant_lifecycle_operations_content_assets_tenant_id_export~",
                        columns: x => new { x.tenant_id, x.export_asset_id },
                        principalTable: "content_assets",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_tenant_lifecycle_operations_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "worker_heartbeats",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    worker_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    processor = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    last_heartbeat_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    last_iteration_started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    last_iteration_completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    last_succeeded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    last_error = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    is_running = table.Column<bool>(type: "boolean", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_worker_heartbeats", x => x.id);
                });

            migrationBuilder.CreateIndex(
                name: "ix_background_jobs_tenant_id_job_type_idempotency_key",
                table: "background_jobs",
                columns: new[] { "tenant_id", "job_type", "idempotency_key" },
                unique: true,
                filter: "idempotency_key is not null");

            migrationBuilder.CreateIndex(
                name: "ix_tenant_lifecycle_operations_tenant_id_export_asset_id",
                table: "tenant_lifecycle_operations",
                columns: new[] { "tenant_id", "export_asset_id" });

            migrationBuilder.CreateIndex(
                name: "ix_tenant_lifecycle_operations_tenant_id_operation_type_status~",
                table: "tenant_lifecycle_operations",
                columns: new[] { "tenant_id", "operation_type", "status", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_worker_heartbeats_processor_last_heartbeat_at",
                table: "worker_heartbeats",
                columns: new[] { "processor", "last_heartbeat_at" });

            migrationBuilder.CreateIndex(
                name: "ix_worker_heartbeats_worker_id_processor",
                table: "worker_heartbeats",
                columns: new[] { "worker_id", "processor" },
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "worker_heartbeats");

            migrationBuilder.DropIndex(
                name: "ix_background_jobs_tenant_id_job_type_idempotency_key",
                table: "background_jobs");

            migrationBuilder.DropColumn(
                name: "cancellation_reason",
                table: "background_jobs");

            migrationBuilder.DropColumn(
                name: "cancellation_requested_at",
                table: "background_jobs");

            migrationBuilder.DropColumn(
                name: "cancellation_requested_by",
                table: "background_jobs");

            migrationBuilder.DropColumn(
                name: "idempotency_key",
                table: "background_jobs");
        }
    }
}
