namespace TaxBaik.Application.Services;

public record TelegramDailyReport(
    DateOnly Date,
    int NewInquiries,
    int PendingInquiries,
    int NewClients,
    int PendingTaxFilings,
    int PendingPayments);

public record TelegramWeeklyReport(
    DateOnly WeekStart,
    DateOnly WeekEnd,
    int NewInquiries,
    int NewClients,
    int UpcomingTaxFilings,
    decimal RevenueThisWeek);

public class TelegramReportService(
    InquiryService inquiryService,
    ClientService clientService,
    TaxFilingScheduleService taxFilingScheduleService,
    RevenueTrackingService revenueTrackingService)
{
    public async Task<TelegramDailyReport> BuildDailyReportAsync(DateOnly date, CancellationToken ct = default)
    {
        var start = date.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
        var end = date.ToDateTime(TimeOnly.MaxValue, DateTimeKind.Utc);

        return new TelegramDailyReport(
            Date: date,
            NewInquiries: await inquiryService.CountByDateRangeAsync(start, end, ct),
            PendingInquiries: await inquiryService.CountByStatusAsync("new", ct),
            NewClients: await clientService.CountCreatedAtRangeAsync(start, end, ct),
            PendingTaxFilings: await taxFilingScheduleService.GetPendingCountAsync(ct),
            PendingPayments: (await revenueTrackingService.GetPendingPaymentsAsync(ct)).Count());
    }

    public async Task<TelegramWeeklyReport> BuildWeeklyReportAsync(DateOnly weekStart, CancellationToken ct = default)
    {
        var weekEnd = weekStart.AddDays(6);
        var start = weekStart.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
        var end = weekEnd.ToDateTime(TimeOnly.MaxValue, DateTimeKind.Utc);
        var upcomingEnd = weekEnd.AddDays(7).ToDateTime(TimeOnly.MaxValue, DateTimeKind.Utc);

        var revenue = await revenueTrackingService.GetTotalRevenueAsync(start, end, ct);

        return new TelegramWeeklyReport(
            WeekStart: weekStart,
            WeekEnd: weekEnd,
            NewInquiries: await inquiryService.CountByDateRangeAsync(start, end, ct),
            NewClients: await clientService.CountCreatedAtRangeAsync(start, end, ct),
            UpcomingTaxFilings: (await taxFilingScheduleService.GetUpcomingDuesAsync(14, ct))
                .Count(x => x.DueDate >= start && x.DueDate <= upcomingEnd),
            RevenueThisWeek: revenue);
    }

    public static string FormatDailyMessage(TelegramDailyReport report) =>
        $"<b>📊 일간 리포트</b>\n\n" +
        $"기준일: <code>{report.Date:yyyy-MM-dd}</code>\n" +
        $"신규 문의: <code>{report.NewInquiries}</code>\n" +
        $"처리 대기 문의: <code>{report.PendingInquiries}</code>\n" +
        $"신규 고객: <code>{report.NewClients}</code>\n" +
        $"신고 대기: <code>{report.PendingTaxFilings}</code>\n" +
        $"미수 청구: <code>{report.PendingPayments}</code>";

    public static string FormatWeeklyMessage(TelegramWeeklyReport report) =>
        $"<b>📈 주간 리포트</b>\n\n" +
        $"기간: <code>{report.WeekStart:yyyy-MM-dd}</code> ~ <code>{report.WeekEnd:yyyy-MM-dd}</code>\n" +
        $"신규 문의: <code>{report.NewInquiries}</code>\n" +
        $"신규 고객: <code>{report.NewClients}</code>\n" +
        $"다가오는 신고: <code>{report.UpcomingTaxFilings}</code>\n" +
        $"주간 매출: <code>₩{report.RevenueThisWeek:N0}</code>";
}
