using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace foodmarket.Infrastructure.Persistence.Migrations
{
    /// <summary>Сотрудники (employees) + роли (employee_roles с JSON-permissions)
    /// + назначения сотрудников на кассы (employee_retail_point_assignments).
    /// Сидер заведёт системные роли и подвяжет существующего admin'а как
    /// сотрудника при первом старте.</summary>
    public partial class Phase4_EmployeesAndRoles : Migration
    {
        protected override void Up(MigrationBuilder b)
        {
            b.CreateTable(
                name: "employee_roles",
                schema: "public",
                columns: table => new
                {
                    Id = table.Column<System.Guid>(type: "uuid", nullable: false),
                    OrganizationId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    IsSystem = table.Column<bool>(type: "boolean", nullable: false),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    permissions = table.Column<string>(type: "jsonb", nullable: true),
                    CreatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_employee_roles", x => x.Id);
                });
            b.CreateIndex(
                name: "IX_employee_roles_OrganizationId_Name",
                schema: "public",
                table: "employee_roles",
                columns: new[] { "OrganizationId", "Name" },
                unique: true);

            b.CreateTable(
                name: "employees",
                schema: "public",
                columns: table => new
                {
                    Id = table.Column<System.Guid>(type: "uuid", nullable: false),
                    OrganizationId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    UserId = table.Column<System.Guid>(type: "uuid", nullable: true),
                    LastName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    FirstName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    MiddleName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    Position = table.Column<string>(type: "character varying(150)", maxLength: 150, nullable: true),
                    Email = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    Phone = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    RoleId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false),
                    FiredAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: true),
                    CreatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_employees", x => x.Id);
                    table.ForeignKey(
                        name: "FK_employees_employee_roles_RoleId",
                        column: x => x.RoleId,
                        principalSchema: "public",
                        principalTable: "employee_roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });
            b.CreateIndex(
                name: "IX_employees_RoleId",
                schema: "public",
                table: "employees",
                column: "RoleId");
            b.CreateIndex(
                name: "IX_employees_OrganizationId_LastName",
                schema: "public",
                table: "employees",
                columns: new[] { "OrganizationId", "LastName" });
            b.CreateIndex(
                name: "IX_employees_OrganizationId_UserId",
                schema: "public",
                table: "employees",
                columns: new[] { "OrganizationId", "UserId" },
                unique: true,
                filter: "\"UserId\" IS NOT NULL");

            b.CreateTable(
                name: "employee_retail_point_assignments",
                schema: "public",
                columns: table => new
                {
                    Id = table.Column<System.Guid>(type: "uuid", nullable: false),
                    OrganizationId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    EmployeeId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    RetailPointId = table.Column<System.Guid>(type: "uuid", nullable: false),
                    CreatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<System.DateTime>(type: "timestamp with time zone", nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_employee_retail_point_assignments", x => x.Id);
                    table.ForeignKey(
                        name: "FK_employee_retail_point_assignments_employees_EmployeeId",
                        column: x => x.EmployeeId,
                        principalSchema: "public",
                        principalTable: "employees",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            b.CreateIndex(
                name: "IX_employee_retail_point_assignments_EmployeeId_RetailPointId",
                schema: "public",
                table: "employee_retail_point_assignments",
                columns: new[] { "EmployeeId", "RetailPointId" },
                unique: true);
        }

        protected override void Down(MigrationBuilder b)
        {
            b.DropTable(name: "employee_retail_point_assignments", schema: "public");
            b.DropTable(name: "employees", schema: "public");
            b.DropTable(name: "employee_roles", schema: "public");
        }
    }
}
