using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ColaFlow.Modules.ProjectManagement.Domain.Aggregates.ProjectAggregate;
using ColaFlow.Modules.ProjectManagement.Domain.ValueObjects;
using ColaFlow.Shared.Kernel.Common;

namespace ColaFlow.Modules.ProjectManagement.Infrastructure.Persistence.Configurations;

/// <summary>
/// Entity configuration for WorkTask entity
/// </summary>
public class WorkTaskConfiguration : IEntityTypeConfiguration<WorkTask>
{
    public void Configure(EntityTypeBuilder<WorkTask> builder)
    {
        builder.ToTable("Tasks");

        // Primary key
        builder.HasKey("Id");

        // Id conversion
        builder.Property(t => t.Id)
            .HasConversion(
                id => id.Value,
                value => TaskId.From(value))
            .IsRequired()
            .ValueGeneratedNever();

        // TenantId (required for multi-tenancy)
        builder.Property(t => t.TenantId)
            .HasConversion(
                id => id.Value,
                value => TenantId.From(value))
            .IsRequired()
            .HasColumnName("tenant_id");

        // StoryId (foreign key)
        builder.Property(t => t.StoryId)
            .HasConversion(
                id => id.Value,
                value => StoryId.From(value))
            .IsRequired();

        // Basic properties
        builder.Property(t => t.Title)
            .HasMaxLength(200)
            .IsRequired();

        builder.Property(t => t.Description)
            .HasMaxLength(4000);

        // Status enumeration
        builder.Property(t => t.Status)
            .HasConversion(
                s => s.Name,
                name => Enumeration.FromDisplayName<WorkItemStatus>(name))
            .HasMaxLength(50)
            .IsRequired();

        // Priority enumeration
        builder.Property(t => t.Priority)
            .HasConversion(
                p => p.Name,
                name => Enumeration.FromDisplayName<TaskPriority>(name))
            .HasMaxLength(50)
            .IsRequired();

        // CreatedBy conversion
        builder.Property(t => t.CreatedBy)
            .HasConversion(
                id => id.Value,
                value => UserId.From(value))
            .IsRequired();

        // AssigneeId (optional)
        builder.Property(t => t.AssigneeId)
            .HasConversion(
                id => id != null ? id.Value : (Guid?)null,
                value => value.HasValue ? UserId.From(value.Value) : null);

        // Effort tracking
        builder.Property(t => t.EstimatedHours);
        builder.Property(t => t.ActualHours);

        // Timestamps
        builder.Property(t => t.CreatedAt)
            .IsRequired();

        builder.Property(t => t.UpdatedAt);

        // Indexes
        builder.HasIndex(t => t.TenantId)
            .HasDatabaseName("ix_tasks_tenant_id");
        builder.HasIndex(t => t.StoryId);
        builder.HasIndex(t => t.AssigneeId);
        builder.HasIndex(t => t.CreatedAt);
    }
}
