using foodmarket.Domain.Organizations;
using Microsoft.EntityFrameworkCore;

namespace foodmarket.Infrastructure.Persistence.Configurations;

public static class OrganizationsHrConfigurations
{
    public static void ConfigureOrganizationsHr(this ModelBuilder b)
    {
        b.Entity<EmployeeRole>(e =>
        {
            e.ToTable("employee_roles");
            e.Property(x => x.Name).HasMaxLength(100).IsRequired();
            e.Property(x => x.Description).HasMaxLength(500);
            // Permissions — owned JSON-колонка. Один объект = один сериализованный
            // блоб на роль. Все поля булевы, по умолчанию false.
            e.OwnsOne(x => x.Permissions, p => p.ToJson("permissions"));
            e.HasIndex(x => new { x.OrganizationId, x.Name }).IsUnique();
        });

        b.Entity<Employee>(e =>
        {
            e.ToTable("employees");
            e.Property(x => x.LastName).HasMaxLength(100).IsRequired();
            e.Property(x => x.FirstName).HasMaxLength(100).IsRequired();
            e.Property(x => x.MiddleName).HasMaxLength(100);
            e.Property(x => x.Position).HasMaxLength(150);
            e.Property(x => x.Email).HasMaxLength(200);
            e.Property(x => x.Phone).HasMaxLength(50);
            e.Property(x => x.Salary).HasPrecision(18, 2);
            e.Property(x => x.TaxNumber).HasMaxLength(20);
            e.Property(x => x.Description).HasMaxLength(2000);
            e.Property(x => x.ImageUrl).HasMaxLength(500);
            e.HasOne(x => x.Role).WithMany().HasForeignKey(x => x.RoleId).OnDelete(DeleteBehavior.Restrict);
            e.HasMany(x => x.RetailPointAssignments).WithOne(a => a.Employee)
                .HasForeignKey(a => a.EmployeeId).OnDelete(DeleteBehavior.Cascade);
            e.HasIndex(x => new { x.OrganizationId, x.UserId }).IsUnique()
                .HasFilter("\"UserId\" IS NOT NULL");
            e.HasIndex(x => new { x.OrganizationId, x.LastName });
        });

        b.Entity<EmployeeRetailPointAssignment>(e =>
        {
            e.ToTable("employee_retail_point_assignments");
            e.HasIndex(x => new { x.EmployeeId, x.RetailPointId }).IsUnique();
        });
    }
}
