using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Office;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 39 G-O4+G-O5+G-O6 (S38) — 5 entity Workflow Apps skeleton.
// Cookie-cutter mirror Proposal pattern (Mig 38). Status flat WorkflowAppStatus 5-state.
// All share workflow V2 ApprovalWorkflowId pin (Mig 37 enum extend +5/+6/+7 done).
// Skeleton Phase 1: NO LevelOpinions table (defer Phase 11 — share global table OR per-module).

public class LeaveRequestConfiguration : IEntityTypeConfiguration<LeaveRequest>
{
    public void Configure(EntityTypeBuilder<LeaveRequest> e)
    {
        e.ToTable("LeaveRequests");
        e.Property(x => x.MaDonTu).HasMaxLength(50);
        e.Property(x => x.RequesterFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.Reason).HasMaxLength(1000).IsRequired();
        e.Property(x => x.NumDays).HasColumnType("decimal(5,2)");
        e.Property(x => x.Status).HasConversion<int>();
        e.HasIndex(x => x.MaDonTu).IsUnique().HasFilter("[MaDonTu] IS NOT NULL");
        e.HasIndex(x => x.RequesterUserId);
        e.HasIndex(x => x.Status);
    }
}

public class OtRequestConfiguration : IEntityTypeConfiguration<OtRequest>
{
    public void Configure(EntityTypeBuilder<OtRequest> e)
    {
        e.ToTable("OtRequests");
        e.Property(x => x.MaDonTu).HasMaxLength(50);
        e.Property(x => x.RequesterFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.Reason).HasMaxLength(1000).IsRequired();
        e.Property(x => x.Hours).HasColumnType("decimal(5,2)");
        e.Property(x => x.Status).HasConversion<int>();
        e.HasIndex(x => x.MaDonTu).IsUnique().HasFilter("[MaDonTu] IS NOT NULL");
        e.HasIndex(x => x.RequesterUserId);
        e.HasIndex(x => x.Status);
    }
}

public class TravelRequestConfiguration : IEntityTypeConfiguration<TravelRequest>
{
    public void Configure(EntityTypeBuilder<TravelRequest> e)
    {
        e.ToTable("TravelRequests");
        e.Property(x => x.MaDonTu).HasMaxLength(50);
        e.Property(x => x.RequesterFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.Destination).HasMaxLength(300).IsRequired();
        e.Property(x => x.Purpose).HasMaxLength(1000).IsRequired();
        e.Property(x => x.EstimatedCost).HasColumnType("decimal(18,2)");
        e.Property(x => x.Status).HasConversion<int>();
        e.HasIndex(x => x.MaDonTu).IsUnique().HasFilter("[MaDonTu] IS NOT NULL");
        e.HasIndex(x => x.RequesterUserId);
        e.HasIndex(x => x.Status);
    }
}

public class VehicleBookingConfiguration : IEntityTypeConfiguration<VehicleBooking>
{
    public void Configure(EntityTypeBuilder<VehicleBooking> e)
    {
        e.ToTable("VehicleBookings");
        e.Property(x => x.MaDonTu).HasMaxLength(50);
        e.Property(x => x.RequesterFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.VehicleLicense).HasMaxLength(20).IsRequired();
        e.Property(x => x.VehicleName).HasMaxLength(200);
        e.Property(x => x.Destination).HasMaxLength(300).IsRequired();
        e.Property(x => x.Purpose).HasMaxLength(1000).IsRequired();
        e.Property(x => x.DriverName).HasMaxLength(200);
        e.Property(x => x.Status).HasConversion<int>();
        e.HasIndex(x => x.MaDonTu).IsUnique().HasFilter("[MaDonTu] IS NOT NULL");
        e.HasIndex(x => x.RequesterUserId);
        e.HasIndex(x => x.Status);
        e.HasIndex(x => new { x.VehicleLicense, x.StartAt });   // overlap check defer Phase 11
    }
}

public class ItTicketConfiguration : IEntityTypeConfiguration<ItTicket>
{
    public void Configure(EntityTypeBuilder<ItTicket> e)
    {
        e.ToTable("ItTickets");
        e.Property(x => x.MaTicket).HasMaxLength(50);
        e.Property(x => x.RequesterFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.Title).HasMaxLength(300).IsRequired();
        e.Property(x => x.Description).HasMaxLength(5000).IsRequired();
        e.Property(x => x.AssignedToFullName).HasMaxLength(200);
        e.Property(x => x.Resolution).HasMaxLength(5000);
        e.Property(x => x.Category).HasConversion<int>();
        e.Property(x => x.Priority).HasConversion<int>();
        e.Property(x => x.Status).HasConversion<int>();
        e.HasIndex(x => x.MaTicket).IsUnique().HasFilter("[MaTicket] IS NOT NULL");
        e.HasIndex(x => x.RequesterUserId);
        e.HasIndex(x => x.AssignedToUserId);
        e.HasIndex(x => x.Status);
        e.HasIndex(x => x.Category);
    }
}

// Mig 40 G-P1 — Attendance
public class AttendanceConfiguration : IEntityTypeConfiguration<Attendance>
{
    public void Configure(EntityTypeBuilder<Attendance> e)
    {
        e.ToTable("Attendances");
        e.Property(x => x.UserFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.CheckInLatitude).HasColumnType("decimal(10,7)");
        e.Property(x => x.CheckInLongitude).HasColumnType("decimal(10,7)");
        e.Property(x => x.CheckInAccuracy).HasColumnType("decimal(8,2)");
        e.Property(x => x.CheckOutLatitude).HasColumnType("decimal(10,7)");
        e.Property(x => x.CheckOutLongitude).HasColumnType("decimal(10,7)");
        e.Property(x => x.CheckOutAccuracy).HasColumnType("decimal(8,2)");
        e.Property(x => x.IpAddressIn).HasMaxLength(50);
        e.Property(x => x.IpAddressOut).HasMaxLength(50);
        e.Property(x => x.Note).HasMaxLength(500);
        e.Property(x => x.WorkHours).HasColumnType("decimal(5,2)");
        e.Property(x => x.OtHours).HasColumnType("decimal(5,2)");
        e.Property(x => x.SourceIn).HasConversion<int>();
        e.Property(x => x.SourceOut).HasConversion<int>();

        // UNIQUE composite (UserId, AttendanceDate)
        e.HasIndex(x => new { x.UserId, x.AttendanceDate }).IsUnique();
    }
}
