using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddClinicalSyncInfrastructure : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<Guid>(
                name: "client_alert_id",
                table: "clinical_alerts",
                type: "uuid",
                nullable: true);

            migrationBuilder.AddColumn<bool>(
                name: "synced_from_gateway",
                table: "clinical_alerts",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "clinical_sites",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    site_code = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    address = table.Column<string>(type: "text", nullable: true),
                    active = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_clinical_sites", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "ward_gateways",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    site_id = table.Column<Guid>(type: "uuid", nullable: false),
                    gateway_code = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    department = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    status = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false, defaultValueSql: "'OFFLINE'"),
                    reported_buffer_depth = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    last_heartbeat_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    last_sync_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ward_gateways", x => x.id);
                    table.CheckConstraint("chk_ward_gateways_status", "status IN ('ONLINE','DEGRADED','OFFLINE')");
                    table.ForeignKey(
                        name: "FK_ward_gateways_clinical_sites_site_id",
                        column: x => x.site_id,
                        principalTable: "clinical_sites",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "clinical_sync_batches",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    gateway_id = table.Column<Guid>(type: "uuid", nullable: false),
                    site_id = table.Column<Guid>(type: "uuid", nullable: false),
                    batch_reference = table.Column<Guid>(type: "uuid", nullable: false),
                    status = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false, defaultValueSql: "'RECEIVED'"),
                    payload = table.Column<string>(type: "jsonb", nullable: false),
                    submitted_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()"),
                    processed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_clinical_sync_batches", x => x.id);
                    table.CheckConstraint("chk_clinical_sync_batches_status", "status IN ('RECEIVED','PROCESSING','APPLIED','CONFLICT','REJECTED')");
                    table.ForeignKey(
                        name: "FK_clinical_sync_batches_clinical_sites_site_id",
                        column: x => x.site_id,
                        principalTable: "clinical_sites",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_clinical_sync_batches_ward_gateways_gateway_id",
                        column: x => x.gateway_id,
                        principalTable: "ward_gateways",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "clinical_sync_conflicts",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: false),
                    client_ref = table.Column<Guid>(type: "uuid", nullable: false),
                    item_type = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
                    conflict_reason = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_clinical_sync_conflicts", x => x.id);
                    table.ForeignKey(
                        name: "FK_clinical_sync_conflicts_clinical_sync_batches_batch_id",
                        column: x => x.batch_id,
                        principalTable: "clinical_sync_batches",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_clinical_alerts_client_alert_id",
                table: "clinical_alerts",
                column: "client_alert_id",
                unique: true,
                filter: "client_alert_id IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_clinical_sites_site_code",
                table: "clinical_sites",
                column: "site_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_clinical_sync_batches_batch_reference",
                table: "clinical_sync_batches",
                column: "batch_reference",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_clinical_sync_batches_gateway_id_submitted_at",
                table: "clinical_sync_batches",
                columns: new[] { "gateway_id", "submitted_at" });

            migrationBuilder.CreateIndex(
                name: "IX_clinical_sync_batches_site_id",
                table: "clinical_sync_batches",
                column: "site_id");

            migrationBuilder.CreateIndex(
                name: "IX_clinical_sync_conflicts_batch_id",
                table: "clinical_sync_conflicts",
                column: "batch_id");

            migrationBuilder.CreateIndex(
                name: "IX_ward_gateways_site_id_department",
                table: "ward_gateways",
                columns: new[] { "site_id", "department" });

            migrationBuilder.CreateIndex(
                name: "IX_ward_gateways_site_id_gateway_code",
                table: "ward_gateways",
                columns: new[] { "site_id", "gateway_code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ward_gateways_status",
                table: "ward_gateways",
                column: "status",
                filter: "status != 'ONLINE'");
        }

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

            migrationBuilder.DropTable(
                name: "clinical_sync_batches");

            migrationBuilder.DropTable(
                name: "ward_gateways");

            migrationBuilder.DropTable(
                name: "clinical_sites");

            migrationBuilder.DropIndex(
                name: "IX_clinical_alerts_client_alert_id",
                table: "clinical_alerts");

            migrationBuilder.DropColumn(
                name: "client_alert_id",
                table: "clinical_alerts");

            migrationBuilder.DropColumn(
                name: "synced_from_gateway",
                table: "clinical_alerts");
        }
    }
}
