using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Baya.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class MessagingAndPartnerCenters : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "partner");

            migrationBuilder.EnsureSchema(
                name: "messaging");

            migrationBuilder.CreateTable(
                name: "PartnerCenters",
                schema: "partner",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: false),
                    LegalEntityType = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: true),
                    MohEstablishmentPermitNo = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
                    TechnicalDirectorNurseUserId = table.Column<int>(type: "int", nullable: true),
                    TechnicalDirectorLicenseNo = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    EnamadCode = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true),
                    SettlementIban = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                    IsMerchantOfRecord = table.Column<bool>(type: "bit", nullable: false),
                    CommissionRate = table.Column<decimal>(type: "decimal(5,4)", precision: 5, scale: 4, nullable: true),
                    AdminUserId = table.Column<int>(type: "int", nullable: false),
                    IsActive = table.Column<bool>(type: "bit", nullable: false),
                    VerifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PartnerCenters", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PartnerCenters_Users_AdminUserId",
                        column: x => x.AdminUserId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_PartnerCenters_Users_TechnicalDirectorNurseUserId",
                        column: x => x.TechnicalDirectorNurseUserId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId");
                });

            migrationBuilder.CreateTable(
                name: "Tickets",
                schema: "messaging",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ReferenceCode = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: false),
                    Subject = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: true),
                    Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    Category = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false),
                    BookingId = table.Column<long>(type: "bigint", nullable: true),
                    RefundId = table.Column<long>(type: "bigint", nullable: true),
                    OpenedById = table.Column<int>(type: "int", nullable: false),
                    ClosedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    ClosedById = table.Column<int>(type: "int", nullable: true),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Tickets", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Tickets_Bookings_BookingId",
                        column: x => x.BookingId,
                        principalSchema: "booking",
                        principalTable: "Bookings",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_Tickets_Refunds_RefundId",
                        column: x => x.RefundId,
                        principalSchema: "payments",
                        principalTable: "Refunds",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_Tickets_Users_ClosedById",
                        column: x => x.ClosedById,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId");
                    table.ForeignKey(
                        name: "FK_Tickets_Users_OpenedById",
                        column: x => x.OpenedById,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "TicketMessages",
                schema: "messaging",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TicketId = table.Column<long>(type: "bigint", nullable: false),
                    SenderId = table.Column<int>(type: "int", nullable: false),
                    Body = table.Column<string>(type: "nvarchar(4000)", maxLength: 4000, nullable: false),
                    IsInternal = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    SentAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TicketMessages", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TicketMessages_Tickets_TicketId",
                        column: x => x.TicketId,
                        principalSchema: "messaging",
                        principalTable: "Tickets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TicketMessages_Users_SenderId",
                        column: x => x.SenderId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "TicketParticipants",
                schema: "messaging",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TicketId = table.Column<long>(type: "bigint", nullable: false),
                    UserId = table.Column<int>(type: "int", nullable: false),
                    RoleOnTicket = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true),
                    AddedById = table.Column<int>(type: "int", nullable: true),
                    RemovedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TicketParticipants", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TicketParticipants_Tickets_TicketId",
                        column: x => x.TicketId,
                        principalSchema: "messaging",
                        principalTable: "Tickets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TicketParticipants_Users_AddedById",
                        column: x => x.AddedById,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId");
                    table.ForeignKey(
                        name: "FK_TicketParticipants_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_NurseProfiles_PartnerCenterId",
                schema: "usr",
                table: "NurseProfiles",
                column: "PartnerCenterId");

            migrationBuilder.CreateIndex(
                name: "IX_PartnerCenters_AdminUserId",
                schema: "partner",
                table: "PartnerCenters",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_PartnerCenters_IsActive",
                schema: "partner",
                table: "PartnerCenters",
                column: "IsActive");

            migrationBuilder.CreateIndex(
                name: "IX_PartnerCenters_TechnicalDirectorNurseUserId",
                schema: "partner",
                table: "PartnerCenters",
                column: "TechnicalDirectorNurseUserId");

            migrationBuilder.CreateIndex(
                name: "IX_TicketMessages_SenderId",
                schema: "messaging",
                table: "TicketMessages",
                column: "SenderId");

            migrationBuilder.CreateIndex(
                name: "IX_TicketMessages_TicketId_SentAt",
                schema: "messaging",
                table: "TicketMessages",
                columns: new[] { "TicketId", "SentAt" });

            migrationBuilder.CreateIndex(
                name: "IX_TicketParticipants_AddedById",
                schema: "messaging",
                table: "TicketParticipants",
                column: "AddedById");

            migrationBuilder.CreateIndex(
                name: "IX_TicketParticipants_TicketId_UserId",
                schema: "messaging",
                table: "TicketParticipants",
                columns: new[] { "TicketId", "UserId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TicketParticipants_UserId_TicketId",
                schema: "messaging",
                table: "TicketParticipants",
                columns: new[] { "UserId", "TicketId" });

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_BookingId",
                schema: "messaging",
                table: "Tickets",
                column: "BookingId");

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_ClosedById",
                schema: "messaging",
                table: "Tickets",
                column: "ClosedById");

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_OpenedById",
                schema: "messaging",
                table: "Tickets",
                column: "OpenedById");

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_ReferenceCode",
                schema: "messaging",
                table: "Tickets",
                column: "ReferenceCode",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_RefundId",
                schema: "messaging",
                table: "Tickets",
                column: "RefundId");

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_Status",
                schema: "messaging",
                table: "Tickets",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_Tickets_Status_CreatedAt",
                schema: "messaging",
                table: "Tickets",
                columns: new[] { "Status", "CreatedAt" });

            migrationBuilder.AddForeignKey(
                name: "FK_NurseProfiles_PartnerCenters_PartnerCenterId",
                schema: "usr",
                table: "NurseProfiles",
                column: "PartnerCenterId",
                principalSchema: "partner",
                principalTable: "PartnerCenters",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_NurseProfiles_PartnerCenters_PartnerCenterId",
                schema: "usr",
                table: "NurseProfiles");

            migrationBuilder.DropTable(
                name: "PartnerCenters",
                schema: "partner");

            migrationBuilder.DropTable(
                name: "TicketMessages",
                schema: "messaging");

            migrationBuilder.DropTable(
                name: "TicketParticipants",
                schema: "messaging");

            migrationBuilder.DropTable(
                name: "Tickets",
                schema: "messaging");

            migrationBuilder.DropIndex(
                name: "IX_NurseProfiles_PartnerCenterId",
                schema: "usr",
                table: "NurseProfiles");
        }
    }
}
