using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Setting.Currency.Cqrs;

public class GetCurrencyListResponse
{
    public string? Id { get; set; }
    public string? Code { get; set; }
    public string? Name { get; set; }
    public string? Symbol { get; set; }
    public string? CountryOwner { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetCurrencyListQuery() : IRequest<List<GetCurrencyListResponse>>;

public class GetCurrencyListHandler : IRequestHandler<GetCurrencyListQuery, List<GetCurrencyListResponse>>
{
    private readonly AppDbContext _context;

    public GetCurrencyListHandler(AppDbContext context) => _context = context;

    public async Task<List<GetCurrencyListResponse>> Handle(GetCurrencyListQuery request, CancellationToken cancellationToken)
    {
        return await _context.Currency
            .AsNoTracking()
            .OrderBy(x => x.Code)
            .Select(x => new GetCurrencyListResponse
            {
                Id = x.Id,
                Code = x.Code,
                Name = x.Name,
                Symbol = x.Symbol,
                CountryOwner = x.CountryOwner,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .ToListAsync(cancellationToken);
    }
}