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

namespace Indotalent.Features.Inventory.Warehouse.Cqrs;

public class GetWarehouseByIdResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
    public bool? SystemWarehouse { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetWarehouseByIdQuery(string Id) : IRequest<GetWarehouseByIdResponse?>;

public class GetWarehouseByIdHandler : IRequestHandler<GetWarehouseByIdQuery, GetWarehouseByIdResponse?>
{
    private readonly AppDbContext _context;

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

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