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

namespace Indotalent.Features.Pipeline.Budget.Cqrs;

public class GetBudgetByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Title { get; set; }
    public string? Description { get; set; }
    public DateTime? BudgetDate { get; set; }
    public BudgetStatus Status { get; set; }
    public decimal? Amount { get; set; }
    public string? CampaignId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetBudgetByIdQuery(string Id) : IRequest<GetBudgetByIdResponse?>;

public class GetBudgetByIdHandler : IRequestHandler<GetBudgetByIdQuery, GetBudgetByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetBudgetByIdResponse?> Handle(GetBudgetByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Budget
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetBudgetByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Title = x.Title,
                Description = x.Description,
                BudgetDate = x.BudgetDate,
                Status = x.Status,
                Amount = x.Amount,
                CampaignId = x.CampaignId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}