using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using WorkClub.Domain.Entities;

namespace WorkClub.Infrastructure.Data.Configurations;

public class ShiftConfiguration : IEntityTypeConfiguration<Shift>
{
    public void Configure(EntityTypeBuilder<Shift> builder)
    {
        builder.ToTable("shifts");

        builder.HasKey(s => s.Id);

        builder.Property(s => s.TenantId)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(s => s.Title)
            .IsRequired()
            .HasMaxLength(200);

        builder.Property(s => s.Description)
            .HasMaxLength(2000);

        builder.Property(s => s.Location)
            .HasMaxLength(500);

        builder.Property(s => s.StartTime)
            .IsRequired();

        builder.Property(s => s.EndTime)
            .IsRequired();

        builder.Property(s => s.Capacity)
            .IsRequired()
            .HasDefaultValue(1);

        builder.Property(s => s.ClubId)
            .IsRequired();

        builder.Property(s => s.CreatedById)
            .IsRequired();

        builder.Property(s => s.CreatedAt)
            .IsRequired();

        builder.Property(s => s.UpdatedAt)
            .IsRequired();

        builder.Property(s => s.RowVersion)
            .IsRowVersion()
            .HasColumnName("xmin")
            .HasColumnType("xid")
            .ValueGeneratedOnAddOrUpdate();

        builder.HasIndex(s => s.TenantId)
            .HasDatabaseName("ix_shifts_tenant_id");

        builder.HasIndex(s => s.ClubId)
            .HasDatabaseName("ix_shifts_club_id");

        builder.HasIndex(s => s.StartTime)
            .HasDatabaseName("ix_shifts_start_time");
    }
}
