using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Domain.Entities.Donations.ValueObject;

namespace Domain.Entities.Donations;

public class CrewSession
{
    [ForeignKey(nameof(CrewID))]
    public virtual Crew? Crew { get; private set; }

    private readonly List<CrewSessionConsent> _consents = [];
    public IReadOnlyCollection<CrewSessionConsent> Consents => _consents;

    private readonly List<CrewDonationSummary> _summaries = [];
    public IReadOnlyCollection<CrewDonationSummary> Summaries => _summaries;

    [Key]
    public int ID { get; private set; }

    public int CrewID { get; private set; }

    public string Title { get; private set; } = default!;

    public CrewSessionStatus Status { get; private set; } = CrewSessionStatus.Inviting;

    public DateTime? StartedAt { get; private set; }

    public DateTime? EndedAt { get; private set; }

    public int TotalAmount { get; private set; }

    public int TotalDonationCount { get; private set; }

    public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;

    private CrewSession() { }

    public static CrewSession Create(int crewID, string title)
    {
        return new CrewSession
        {
            CrewID = crewID,
            Title = title
        };
    }

    public void Activate()
    {
        if (Status != CrewSessionStatus.Inviting)
        {
            throw new InvalidOperationException("Only inviting sessions can be activated.");
        }

        Status = CrewSessionStatus.Active;
        StartedAt = DateTime.UtcNow;
    }

    public void End()
    {
        if (Status != CrewSessionStatus.Active)
        {
            throw new InvalidOperationException("Only active sessions can be ended.");
        }

        Status = CrewSessionStatus.Ended;
        EndedAt = DateTime.UtcNow;
    }

    public void AddDonation(int amount)
    {
        TotalAmount += amount;
        TotalDonationCount++;
    }
}