namespace CMSMicroservice.Application.WarehouseCQ.Queries.GetWarehouse;

public class GetWarehouseQueryHandler : IRequestHandler<GetWarehouseQuery, GetWarehouseResponseDto?>
{
    private readonly IApplicationDbContext _context;

    public GetWarehouseQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<GetWarehouseResponseDto?> Handle(GetWarehouseQuery request, CancellationToken cancellationToken)
    {
        var warehouse = await _context.Warehouses
            .AsNoTracking()
            .Where(w => w.Id == request.Id)
            .Select(w => new GetWarehouseResponseDto
            {
                Id = w.Id,
                Name = w.Name,
                Code = w.Code,
                Address = w.Address,
                IsDefault = w.IsDefault,
                IsActive = w.IsActive
            })
            .FirstOrDefaultAsync(cancellationToken);

        return warehouse;
    }
}
