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

#nullable disable

namespace ShadowedRealms.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddAdminManagementTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AdminActionAudits",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    AdminUserId = table.Column<int>(type: "integer", maxLength: 450, nullable: false),
                    PlayerId = table.Column<int>(type: "integer", nullable: true),
                    ActionType = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    ActionDetails = table.Column<string>(type: "text", nullable: true),
                    Reason = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    ActionData = table.Column<string>(type: "text", nullable: true),
                    Timestamp = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
                    KingdomId = table.Column<int>(type: "integer", nullable: false),
                    IpAddress = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: true),
                    UserAgent = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    Success = table.Column<bool>(type: "boolean", nullable: false),
                    ErrorMessage = table.Column<string>(type: "text", nullable: true),
                    DurationMs = table.Column<int>(type: "integer", nullable: true),
                    SessionId = table.Column<string>(type: "text", nullable: true),
                    AdditionalData = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AdminActionAudits", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AdminActionAudit_Kingdoms_KingdomId",
                        column: x => x.KingdomId,
                        principalTable: "Kingdoms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_AdminActionAudit_Players_PlayerId",
                        column: x => x.PlayerId,
                        principalTable: "Players",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_AdminActionAudit_Users_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "Players_Auth",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "AdminPlayerMessages",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    PlayerId = table.Column<int>(type: "integer", nullable: false),
                    AdminUserId = table.Column<int>(type: "integer", maxLength: 450, nullable: false),
                    Message = table.Column<string>(type: "text", nullable: false),
                    MessageType = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    SentAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
                    IsRead = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    KingdomId = table.Column<int>(type: "integer", nullable: false),
                    PlayerResponse = table.Column<string>(type: "text", nullable: true),
                    PlayerResponseAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    AdminNotes = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    ParentMessageId = table.Column<long>(type: "bigint", nullable: true),
                    Priority = table.Column<int>(type: "integer", nullable: false),
                    IsAutomaticMessage = table.Column<bool>(type: "boolean", nullable: false),
                    TemplateUsed = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AdminPlayerMessages", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AdminPlayerMessages_Kingdoms_KingdomId",
                        column: x => x.KingdomId,
                        principalTable: "Kingdoms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_AdminPlayerMessages_Players_PlayerId",
                        column: x => x.PlayerId,
                        principalTable: "Players",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AdminPlayerMessages_Users_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "Players_Auth",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "AdminSessions",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    AdminUserId = table.Column<int>(type: "integer", maxLength: 450, nullable: false),
                    LoginTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
                    LogoutTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    IpAddress = table.Column<string>(type: "character varying(45)", maxLength: 45, nullable: true),
                    UserAgent = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    Location = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    SessionId = table.Column<string>(type: "text", nullable: true),
                    LoginMethod = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    RiskLevel = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
                    RiskFactors = table.Column<string>(type: "text", nullable: true),
                    DeviceFingerprint = table.Column<string>(type: "text", nullable: true),
                    SessionData = table.Column<string>(type: "text", nullable: true),
                    ForcedLogout = table.Column<bool>(type: "boolean", nullable: false),
                    LogoutReason = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    ActionsPerformed = table.Column<int>(type: "integer", nullable: false),
                    LastActivity = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    SessionDurationMinutes = table.Column<int>(type: "integer", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AdminSessions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AdminSessions_Users_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "Players_Auth",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_ActionType",
                table: "AdminActionAudits",
                column: "ActionType");

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_Admin_Time",
                table: "AdminActionAudits",
                columns: new[] { "AdminUserId", "Timestamp" });

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_AdminUserId",
                table: "AdminActionAudits",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_Kingdom_Time",
                table: "AdminActionAudits",
                columns: new[] { "KingdomId", "Timestamp" });

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_KingdomId",
                table: "AdminActionAudits",
                column: "KingdomId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_PlayerId",
                table: "AdminActionAudits",
                column: "PlayerId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminActionAudit_Timestamp",
                table: "AdminActionAudits",
                column: "Timestamp");

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_AdminUserId",
                table: "AdminPlayerMessages",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_Kingdom_Time",
                table: "AdminPlayerMessages",
                columns: new[] { "KingdomId", "SentAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_KingdomId",
                table: "AdminPlayerMessages",
                column: "KingdomId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_Player_Time",
                table: "AdminPlayerMessages",
                columns: new[] { "PlayerId", "SentAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_PlayerId",
                table: "AdminPlayerMessages",
                column: "PlayerId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminPlayerMessages_SentAt",
                table: "AdminPlayerMessages",
                column: "SentAt");

            migrationBuilder.CreateIndex(
                name: "IX_AdminSessions_AdminUserId",
                table: "AdminSessions",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_AdminSessions_LoginTime",
                table: "AdminSessions",
                column: "LoginTime");

            migrationBuilder.CreateIndex(
                name: "IX_AdminSessions_User_LoginTime",
                table: "AdminSessions",
                columns: new[] { "AdminUserId", "LoginTime" });
        }

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

            migrationBuilder.DropTable(
                name: "AdminPlayerMessages");

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