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

namespace Indotalent.Features.Pipeline.Campaign.Cqrs;

public class CreateCampaignRequest
{
    public string? Title { get; set; }
    public string? Description { get; set; }
    public decimal? TargetRevenueAmount { get; set; }
    public DateTime? CampaignDateStart { get; set; }
    public DateTime? CampaignDateFinish { get; set; }
    public CampaignStatus Status { get; set; }
    public string? SalesTeamId { get; set; }
}

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

public record CreateCampaignCommand(CreateCampaignRequest Data) : IRequest<CreateCampaignResponse>;

public class CreateCampaignHandler : IRequestHandler<CreateCampaignCommand, CreateCampaignResponse>
{
    private readonly AppDbContext _context;
    public CreateCampaignHandler(AppDbContext context) => _context = context;

    public async Task<CreateCampaignResponse> Handle(CreateCampaignCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.Campaign
        {
            Title = request.Data.Title,
            Description = request.Data.Description,
            TargetRevenueAmount = request.Data.TargetRevenueAmount,
            CampaignDateStart = request.Data.CampaignDateStart,
            CampaignDateFinish = request.Data.CampaignDateFinish,
            Status = request.Data.Status,
            SalesTeamId = request.Data.SalesTeamId
        };

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

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