using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.CreateTable(
                name: "NurseSearchIndices",
                schema: "search",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    VariantId = table.Column<long>(type: "bigint", nullable: false),
                    NurseId = table.Column<long>(type: "bigint", nullable: false),
                    ServiceCategoryId = table.Column<long>(type: "bigint", nullable: false),
                    Price = table.Column<long>(type: "bigint", nullable: false),
                    PriceUnit = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    CityId = table.Column<long>(type: "bigint", nullable: false),
                    DistrictId = table.Column<long>(type: "bigint", nullable: true),
                    NurseGender = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: true),
                    AverageRating = table.Column<decimal>(type: "decimal(3,2)", precision: 3, scale: 2, nullable: false),
                    TotalReviews = table.Column<int>(type: "int", nullable: false),
                    TotalCompletedBookings = table.Column<int>(type: "int", nullable: false),
                    IsSearchable = table.Column<bool>(type: "bit", nullable: false),
                    UpdatedAt = 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_NurseSearchIndices", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NurseSearchIndices_NurseProfiles_NurseId",
                        column: x => x.NurseId,
                        principalSchema: "usr",
                        principalTable: "NurseProfiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NurseSearchIndices_NurseServiceVariants_VariantId",
                        column: x => x.VariantId,
                        principalSchema: "catalog",
                        principalTable: "NurseServiceVariants",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_NurseSearchIndex_Search",
                schema: "search",
                table: "NurseSearchIndices",
                columns: new[] { "IsSearchable", "ServiceCategoryId", "CityId", "DistrictId" })
                .Annotation("SqlServer:Include", new[] { "Price", "NurseGender", "AverageRating", "TotalReviews", "NurseId", "VariantId" });

            migrationBuilder.CreateIndex(
                name: "IX_NurseSearchIndices_NurseId",
                schema: "search",
                table: "NurseSearchIndices",
                column: "NurseId");

            migrationBuilder.CreateIndex(
                name: "UX_NurseSearchIndex_Variant_City_District",
                schema: "search",
                table: "NurseSearchIndices",
                columns: new[] { "VariantId", "CityId", "DistrictId" },
                unique: true,
                filter: "[DistrictId] IS NOT NULL AND [DeletedAt] IS NULL");

            migrationBuilder.CreateIndex(
                name: "UX_NurseSearchIndex_Variant_City_WholeCity",
                schema: "search",
                table: "NurseSearchIndices",
                columns: new[] { "VariantId", "CityId" },
                unique: true,
                filter: "[DistrictId] IS NULL AND [DeletedAt] IS NULL");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "NurseSearchIndices",
                schema: "search");
        }
    }
}
