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

namespace Indotalent.Features.Thirdparty.CustomerCategory.Cqrs;

public class GetCustomerCategoryListResponse
{
    public string? Id { get; set; }
    public string? Name { 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 GetCustomerCategoryListQuery() : IRequest<List<GetCustomerCategoryListResponse>>;

public class GetCustomerCategoryListHandler : IRequestHandler<GetCustomerCategoryListQuery, List<GetCustomerCategoryListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetCustomerCategoryListResponse>> Handle(GetCustomerCategoryListQuery request, CancellationToken cancellationToken)
    {
        return await _context.CustomerCategory
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new GetCustomerCategoryListResponse
            {
                Id = x.Id,
                Name = x.Name,
                Description = x.Description,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .ToListAsync(cancellationToken);
    }
}