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

namespace Indotalent.Features.Thirdparty.VendorContact.Cqrs;

public class GetVendorContactByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Name { get; set; }
    public string? JobTitle { get; set; }
    public string? PhoneNumber { get; set; }
    public string? EmailAddress { get; set; }
    public string? Description { get; set; }
    public string? VendorId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetVendorContactByIdQuery(string Id) : IRequest<GetVendorContactByIdResponse?>;

public class GetVendorContactByIdHandler : IRequestHandler<GetVendorContactByIdQuery, GetVendorContactByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetVendorContactByIdResponse?> Handle(GetVendorContactByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.VendorContact
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetVendorContactByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Name = x.Name,
                JobTitle = x.JobTitle,
                PhoneNumber = x.PhoneNumber,
                EmailAddress = x.EmailAddress,
                Description = x.Description,
                VendorId = x.VendorId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}