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

namespace Domain.Entities.Donations;

public class Settlement
{
    [ForeignKey(nameof(ChannelID))]
    public virtual Channel? Channel { get; private set; }

    [ForeignKey(nameof(MemberID))]
    public virtual Member? Member { get; private set; }

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

    public int ChannelID { get; private set; }

    public int MemberID { get; private set; }

    public DateTime PeriodStart { get; private set; }

    public DateTime PeriodEnd { get; private set; }

    public int GrossAmount { get; private set; }

    public int FeeAmount { get; private set; }

    public int NetAmount { get; private set; }

    public SettlementStatus Status { get; private set; } = SettlementStatus.Pending;

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

    public DateTime? ApprovedAt { get; private set; }

    public DateTime? PaidAt { get; private set; }

    public string? AdminMemo { get; private set; }

    private Settlement() { }

    public static Settlement Create(
        int channelID,
        int memberID,
        DateTime periodStart,
        DateTime periodEnd,
        int grossAmount,
        int feeAmount,
        int netAmount
    ) {
        return new Settlement
        {
            ChannelID = channelID,
            MemberID = memberID,
            PeriodStart = periodStart,
            PeriodEnd = periodEnd,
            GrossAmount = grossAmount,
            FeeAmount = feeAmount,
            NetAmount = netAmount
        };
    }

    public void Approve(string? adminMemo = null)
    {
        Status = SettlementStatus.Approved;
        ApprovedAt = DateTime.UtcNow;
        AdminMemo = adminMemo;
    }

    public void Reject(string? adminMemo = null)
    {
        Status = SettlementStatus.Rejected;
        AdminMemo = adminMemo;
    }

    public void MarkPaid()
    {
        Status = SettlementStatus.Paid;
        PaidAt = DateTime.UtcNow;
    }
}