using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Meezi.Infrastructure.Data.Migrations
{
    /// <inheritdoc />
    public partial class AddEmployeeBranchRole : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "EmployeeBranchRoles",
                columns: table => new
                {
                    Id = table.Column<string>(type: "text", nullable: false),
                    EmployeeId = table.Column<string>(type: "text", nullable: false),
                    BranchId = table.Column<string>(type: "text", nullable: false),
                    Role = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    CafeId = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EmployeeBranchRoles", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EmployeeBranchRoles_Branches_BranchId",
                        column: x => x.BranchId,
                        principalTable: "Branches",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_EmployeeBranchRoles_Employees_EmployeeId",
                        column: x => x.EmployeeId,
                        principalTable: "Employees",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_EmployeeBranchRoles_BranchId",
                table: "EmployeeBranchRoles",
                column: "BranchId");

            migrationBuilder.CreateIndex(
                name: "IX_EmployeeBranchRoles_CafeId_BranchId",
                table: "EmployeeBranchRoles",
                columns: new[] { "CafeId", "BranchId" });

            migrationBuilder.CreateIndex(
                name: "IX_EmployeeBranchRoles_EmployeeId_BranchId",
                table: "EmployeeBranchRoles",
                columns: new[] { "EmployeeId", "BranchId" },
                unique: true,
                filter: "\"DeletedAt\" IS NULL");

            // Backfill: every existing branch-pinned, non-owner employee gets an
            // explicit per-branch role row mirroring their current (BranchId, Role).
            // Owners (Role = 0) and café-wide non-pinned staff (BranchId IS NULL) are
            // left untouched — they remain café-wide via Employee.Role.
            migrationBuilder.Sql(@"
                INSERT INTO ""EmployeeBranchRoles""
                    (""Id"", ""EmployeeId"", ""BranchId"", ""Role"", ""CafeId"", ""CreatedAt"")
                SELECT replace(gen_random_uuid()::text, '-', ''),
                       e.""Id"", e.""BranchId"", e.""Role"", e.""CafeId"", now()
                FROM ""Employees"" e
                WHERE e.""BranchId"" IS NOT NULL
                  AND e.""DeletedAt"" IS NULL
                  AND e.""Role"" <> 0;
            ");
        }

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