namespace TaxBaik.Infrastructure.Repositories;

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

public class RevenueTrackingRepository(IDbConnectionFactory connectionFactory) : BaseRepository(connectionFactory), IRevenueTrackingRepository
{
    public async Task<int> CreateAsync(RevenueTracking revenue, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        return await conn.QueryFirstAsync<int>(
            @"INSERT INTO revenue_tracking (client_id, invoice_number, invoice_date, service_type, amount, payment_status, payment_date, due_date, notes, created_at, updated_at)
              VALUES (@ClientId, @InvoiceNumber, @InvoiceDate, @ServiceType, @Amount, @PaymentStatus, @PaymentDate, @DueDate, @Notes, NOW(), NOW())
              RETURNING id",
            revenue);
    }

    public async Task<IEnumerable<RevenueTracking>> GetAllAsync(CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        return await conn.QueryAsync<RevenueTracking>(
            @"SELECT id, client_id, invoice_number, invoice_date, service_type, amount, payment_status, payment_date, due_date, notes, created_at, updated_at
              FROM revenue_tracking ORDER BY invoice_date DESC");
    }

    public async Task<IEnumerable<RevenueTracking>> GetByClientIdAsync(int clientId, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        return await conn.QueryAsync<RevenueTracking>(
            @"SELECT id, client_id, invoice_number, invoice_date, service_type, amount, payment_status, payment_date, due_date, notes, created_at, updated_at
              FROM revenue_tracking WHERE client_id = @ClientId ORDER BY invoice_date DESC",
            new { ClientId = clientId });
    }

    public async Task<IEnumerable<RevenueTracking>> GetPendingPaymentsAsync(CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        return await conn.QueryAsync<RevenueTracking>(
            @"SELECT id, client_id, invoice_number, invoice_date, service_type, amount, payment_status, payment_date, due_date, notes, created_at, updated_at
              FROM revenue_tracking WHERE payment_status = 'pending' ORDER BY due_date ASC");
    }

    public async Task<IEnumerable<RevenueTracking>> GetByDateRangeAsync(DateTime startDate, DateTime endDate, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        return await conn.QueryAsync<RevenueTracking>(
            @"SELECT id, client_id, invoice_number, invoice_date, service_type, amount, payment_status, payment_date, due_date, notes, created_at, updated_at
              FROM revenue_tracking WHERE invoice_date BETWEEN @StartDate AND @EndDate ORDER BY invoice_date DESC",
            new { StartDate = startDate, EndDate = endDate });
    }

    public async Task UpdateAsync(RevenueTracking revenue, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        await conn.ExecuteAsync(
            @"UPDATE revenue_tracking SET invoice_number = @InvoiceNumber, invoice_date = @InvoiceDate,
              service_type = @ServiceType, amount = @Amount, payment_status = @PaymentStatus,
              payment_date = @PaymentDate, due_date = @DueDate, notes = @Notes, updated_at = NOW()
              WHERE id = @Id",
            revenue);
    }

    public async Task MarkPaidAsync(int id, DateTime paymentDate, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        await conn.ExecuteAsync(
            @"UPDATE revenue_tracking SET payment_status = 'paid', payment_date = @PaymentDate, updated_at = NOW() WHERE id = @Id",
            new { Id = id, PaymentDate = paymentDate });
    }

    public async Task<decimal> GetTotalRevenueAsync(DateTime startDate, DateTime endDate, CancellationToken cancellationToken = default)
    {
        using var conn = Conn();
        var result = await conn.QueryFirstAsync<decimal>(
            @"SELECT COALESCE(SUM(amount), 0) FROM revenue_tracking WHERE invoice_date BETWEEN @StartDate AND @EndDate",
            new { StartDate = startDate, EndDate = endDate });
        return result;
    }
}
