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

namespace Indotalent.Features.Utilities.ProgramManager.Cqrs;

public class CreateProgramManagerRequest
{
    public string? Title { get; set; }
    public string? Summary { get; set; }
    public ProgramManagerStatus Status { get; set; }
    public ProgramManagerPriority Priority { get; set; }
    public string? ProgramManagerResourceId { get; set; }
}

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

public record CreateProgramManagerCommand(CreateProgramManagerRequest Data) : IRequest<CreateProgramManagerResponse>;

public class CreateProgramManagerHandler : IRequestHandler<CreateProgramManagerCommand, CreateProgramManagerResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateProgramManagerResponse> Handle(CreateProgramManagerCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.ProgramManager
        {
            Title = request.Data.Title,
            Summary = request.Data.Summary,
            Status = request.Data.Status,
            Priority = request.Data.Priority,
            ProgramManagerResourceId = request.Data.ProgramManagerResourceId
        };

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

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