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

namespace Indotalent.Features.Thirdparty.VendorGroup.Cqrs;

public class GetVendorGroupByIdResponse
{
    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 GetVendorGroupByIdQuery(string Id) : IRequest<GetVendorGroupByIdResponse?>;

public class GetVendorGroupByIdHandler : IRequestHandler<GetVendorGroupByIdQuery, GetVendorGroupByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetVendorGroupByIdResponse?> Handle(GetVendorGroupByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.VendorGroup
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetVendorGroupByIdResponse
            {
                Id = x.Id,
                Name = x.Name,
                Description = x.Description,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}