using Indotalent.Infrastructure.Database;
using MediatR;

namespace Indotalent.Features.Utilities.ProgramResource.Cqrs;

public class CreateProgramResourceRequest
{
    public string? Name { get; set; }
    public string? Description { get; set; }
}

public class CreateProgramResourceResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
}

public record CreateProgramResourceCommand(CreateProgramResourceRequest Data) : IRequest<CreateProgramResourceResponse>;

public class CreateProgramResourceHandler : IRequestHandler<CreateProgramResourceCommand, CreateProgramResourceResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateProgramResourceResponse> Handle(CreateProgramResourceCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.ProgramManagerResource
        {
            Name = request.Data.Name,
            Description = request.Data.Description
        };

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

        return new CreateProgramResourceResponse
        {
            Id = entity.Id,
            Name = entity.Name
        };
    }
}