using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace VigilCareClinicalAPI.Migrations
{
    /// <inheritdoc />
    public partial class AddAlertQualityAnalytics : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "feedback_received",
                table: "clinical_alerts",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "alert_feedbacks",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    alert_id = table.Column<Guid>(type: "uuid", nullable: false),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    feedback_type = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    comment = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_alert_feedbacks", x => x.id);
                    table.ForeignKey(
                        name: "FK_alert_feedbacks_clinical_alerts_alert_id",
                        column: x => x.alert_id,
                        principalTable: "clinical_alerts",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "alert_quality_metrics",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    alert_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    window_start = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    window_end = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
                    total_alerts = table.Column<int>(type: "integer", nullable: false),
                    acknowledged_count = table.Column<int>(type: "integer", nullable: false),
                    resolved_count = table.Column<int>(type: "integer", nullable: false),
                    escalated_count = table.Column<int>(type: "integer", nullable: false),
                    feedback_useful_count = table.Column<int>(type: "integer", nullable: false),
                    feedback_false_positive_count = table.Column<int>(type: "integer", nullable: false),
                    feedback_would_act_count = table.Column<int>(type: "integer", nullable: false),
                    feedback_count = table.Column<int>(type: "integer", nullable: false),
                    acknowledgement_rate = table.Column<double>(type: "double precision", nullable: false),
                    false_positive_rate = table.Column<double>(type: "double precision", nullable: false),
                    useful_rate = table.Column<double>(type: "double precision", nullable: false),
                    would_act_rate = table.Column<double>(type: "double precision", nullable: false),
                    avg_seconds_to_acknowledge = table.Column<double>(type: "double precision", nullable: false),
                    avg_seconds_to_resolution = table.Column<double>(type: "double precision", nullable: false),
                    computed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "NOW()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_alert_quality_metrics", x => x.id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_alert_feedbacks_alert_id",
                table: "alert_feedbacks",
                column: "alert_id");

            migrationBuilder.CreateIndex(
                name: "IX_alert_feedbacks_alert_id_user_id",
                table: "alert_feedbacks",
                columns: new[] { "alert_id", "user_id" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_alert_feedbacks_feedback_type",
                table: "alert_feedbacks",
                column: "feedback_type");

            migrationBuilder.CreateIndex(
                name: "IX_alert_quality_metrics_alert_type_window_start_window_end",
                table: "alert_quality_metrics",
                columns: new[] { "alert_type", "window_start", "window_end" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_alert_quality_metrics_window_start",
                table: "alert_quality_metrics",
                column: "window_start");
        }

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

            migrationBuilder.DropTable(
                name: "alert_quality_metrics");

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