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

namespace Indotalent.Features.Pipeline.Campaign.Cqrs;

public class CreateBudgetRequest
{
    public string? CampaignId { get; set; }
    public string? Title { get; set; }
    public string? Description { get; set; }
    public DateTime? BudgetDate { get; set; }
    public decimal? Amount { get; set; }
    public BudgetStatus Status { get; set; }
}

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

public record CreateBudgetCommand(CreateBudgetRequest Data) : IRequest<CreateBudgetResponse>;

public class CreateBudgetHandler : IRequestHandler<CreateBudgetCommand, CreateBudgetResponse>
{
    private readonly AppDbContext _context;
    public CreateBudgetHandler(AppDbContext context) => _context = context;

    public async Task<CreateBudgetResponse> Handle(CreateBudgetCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.Budget
        {
            CampaignId = request.Data.CampaignId,
            Title = request.Data.Title,
            Description = request.Data.Description,
            BudgetDate = request.Data.BudgetDate,
            Amount = request.Data.Amount,
            Status = request.Data.Status
        };

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

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