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

namespace WorkClub.Infrastructure.Data.Configurations;

public class WorkItemConfiguration : IEntityTypeConfiguration<WorkItem>
{
    public void Configure(EntityTypeBuilder<WorkItem> builder)
    {
        builder.ToTable("work_items");

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

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

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

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

        builder.Property(w => w.Status)
            .IsRequired()
            .HasConversion<int>();

        builder.Property(w => w.AssigneeId);

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

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

        builder.Property(w => w.DueDate);

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

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

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

        builder.HasIndex(w => w.TenantId)
            .HasDatabaseName("ix_work_items_tenant_id");

        builder.HasIndex(w => w.ClubId)
            .HasDatabaseName("ix_work_items_club_id");

        builder.HasIndex(w => w.Status)
            .HasDatabaseName("ix_work_items_status");

        builder.HasIndex(w => w.AssigneeId)
            .HasDatabaseName("ix_work_items_assignee_id");
    }
}
