using CMSMicroservice.Domain.Entities.Club;
using CMSMicroservice.Domain.Entities.History;
using CMSMicroservice.Domain.Enums;

namespace CMSMicroservice.Application.Common.Helpers;

public static class ClubMembershipCycleHistoryWriter
{
    public static void WriteCycleStarted(
        IApplicationDbContext context,
        ClubMembershipCycle cycle,
        string? performedBy = "System",
        string? reason = null)
    {
        context.ClubMembershipCycleHistories.Add(new ClubMembershipCycleHistory
        {
            ClubMembershipCycleId = cycle.Id,
            UserId = cycle.UserId,
            CycleNumber = cycle.CycleNumber,
            OldIsCurrentCycle = false,
            NewIsCurrentCycle = cycle.IsCurrentCycle,
            OldMagicStartedAt = null,
            NewMagicStartedAt = cycle.MagicStartedAt,
            OldMagicCompletedAt = null,
            NewMagicCompletedAt = cycle.MagicCompletedAt,
            Action = ClubMembershipCycleAction.Started,
            PerformedBy = performedBy,
            Reason = reason
        });
    }

    public static void WriteCycleClosed(
        IApplicationDbContext context,
        ClubMembershipCycle cycle,
        string? performedBy = "System",
        string? reason = null)
    {
        context.ClubMembershipCycleHistories.Add(new ClubMembershipCycleHistory
        {
            ClubMembershipCycleId = cycle.Id,
            UserId = cycle.UserId,
            CycleNumber = cycle.CycleNumber,
            OldIsCurrentCycle = true,
            NewIsCurrentCycle = false,
            OldMagicStartedAt = cycle.MagicStartedAt,
            NewMagicStartedAt = cycle.MagicStartedAt,
            OldMagicCompletedAt = cycle.MagicCompletedAt,
            NewMagicCompletedAt = cycle.MagicCompletedAt,
            Action = ClubMembershipCycleAction.Completed,
            PerformedBy = performedBy,
            Reason = reason ?? "دوره جدید جایگزین شد"
        });
    }
}
