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

namespace Indotalent.Features.Pipeline.Campaign.Cqrs;

public class CreateExpenseRequest
{
    public string? CampaignId { get; set; }
    public string? Title { get; set; }
    public string? Description { get; set; }
    public DateTime? ExpenseDate { get; set; }
    public decimal? Amount { get; set; }
    public ExpenseStatus Status { get; set; }
}

public class CreateExpenseResponse
{
    public string? Id { get; set; }
    public string? Title { get; set; }
}

public record CreateExpenseCommand(CreateExpenseRequest Data) : IRequest<CreateExpenseResponse>;

public class CreateExpenseHandler : IRequestHandler<CreateExpenseCommand, CreateExpenseResponse>
{
    private readonly AppDbContext _context;
    public CreateExpenseHandler(AppDbContext context) => _context = context;

    public async Task<CreateExpenseResponse> Handle(CreateExpenseCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.Expense
        {
            CampaignId = request.Data.CampaignId,
            Title = request.Data.Title,
            Description = request.Data.Description,
            ExpenseDate = request.Data.ExpenseDate,
            Amount = request.Data.Amount,
            Status = request.Data.Status
        };

        _context.Expense.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);

        return new CreateExpenseResponse
        {
            Id = entity.Id,
            Title = entity.Title
        };
    }
}