namespace TaxBaik.Application.Services;

using TaxBaik.Domain.Entities;
using TaxBaik.Domain.Interfaces;

public class ConsultingActivityService(IConsultingActivityRepository repository)
{
    public async Task<int> CreateAsync(int clientId, string activityType, DateTime activityDate,
        string description, int? consultantId = null, DateTime? nextFollowupDate = null, CancellationToken ct = default)
    {
        if (clientId <= 0)
            throw new ValidationException("유효한 고객을 선택하세요.");
        if (string.IsNullOrWhiteSpace(activityType))
            throw new ValidationException("활동 유형을 입력하세요.");
        if (string.IsNullOrWhiteSpace(description))
            throw new ValidationException("활동 내용을 입력하세요.");

        var activity = new ConsultingActivity
        {
            ClientId = clientId,
            ActivityType = activityType.Trim(),
            ActivityDate = activityDate,
            Description = description.Trim(),
            AssignedConsultantId = consultantId,
            NextFollowupDate = nextFollowupDate,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow
        };

        return await repository.CreateAsync(activity, ct);
    }

    public async Task<IEnumerable<ConsultingActivity>> GetByClientIdAsync(int clientId, CancellationToken ct = default) =>
        await repository.GetByClientIdAsync(clientId, ct);

    public async Task<IEnumerable<ConsultingActivity>> GetAllAsync(CancellationToken ct = default) =>
        await repository.GetAllAsync(ct);

    public async Task<IEnumerable<ConsultingActivity>> GetPendingFollowupsAsync(CancellationToken ct = default) =>
        await repository.GetPendingFollowupsAsync(ct);

    public async Task<IEnumerable<ConsultingActivity>> GetConsultantActivityAsync(int consultantId, DateTime fromDate, CancellationToken ct = default) =>
        await repository.GetByConsultantAsync(consultantId, fromDate, ct);

    public async Task UpdateAsync(int id, string? outcome, DateTime? nextFollowupDate, CancellationToken ct = default)
    {
        var activity = new ConsultingActivity { Id = id, Outcome = outcome, NextFollowupDate = nextFollowupDate, UpdatedAt = DateTime.UtcNow };
        await repository.UpdateAsync(activity, ct);
    }
}
