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

namespace Indotalent.Features.Setting.Currency.Cqrs;

public class GetCurrencyByIdResponse
{
    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 string? Description { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetCurrencyByIdQuery(string Id) : IRequest<GetCurrencyByIdResponse?>;

public class GetCurrencyByIdHandler : IRequestHandler<GetCurrencyByIdQuery, GetCurrencyByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetCurrencyByIdResponse?> Handle(GetCurrencyByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Currency
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetCurrencyByIdResponse
            {
                Id = x.Id,
                Code = x.Code,
                Name = x.Name,
                Symbol = x.Symbol,
                CountryOwner = x.CountryOwner,
                Description = x.Description,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}