using WorkClub.Domain.Enums;
using WorkClub.Domain.Interfaces;

namespace WorkClub.Domain.Entities;

public class WorkItem : ITenantEntity
{
    public required Guid Id { get; set; }
    public required string TenantId { get; set; }
    public required string Title { get; set; }
    public string? Description { get; set; }
    public required WorkItemStatus Status { get; set; }
    public Guid? AssigneeId { get; set; }
    public required Guid CreatedById { get; set; }
    public required Guid ClubId { get; set; }
    public DateTimeOffset? DueDate { get; set; }
    public required DateTimeOffset CreatedAt { get; set; }
    public required DateTimeOffset UpdatedAt { get; set; }
    public uint RowVersion { get; set; }

    public bool CanTransitionTo(WorkItemStatus newStatus) => (Status, newStatus) switch
    {
        (WorkItemStatus.Open, WorkItemStatus.Assigned) => true,
        (WorkItemStatus.Assigned, WorkItemStatus.InProgress) => true,
        (WorkItemStatus.InProgress, WorkItemStatus.Review) => true,
        (WorkItemStatus.Review, WorkItemStatus.Done) => true,
        (WorkItemStatus.Review, WorkItemStatus.InProgress) => true,
        _ => false
    };

    public void TransitionTo(WorkItemStatus newStatus)
    {
        if (!CanTransitionTo(newStatus))
        {
            throw new InvalidOperationException(
                $"Cannot transition from {Status} to {newStatus}");
        }

        Status = newStatus;
    }
}
