namespace Adapters.Driving.Api.Types;

using Application.Enrollments.Commands;
using Application.Students.Commands;
using Application.Students.DTOs;
using MediatR;

public class Mutation
{
    [GraphQLDescription("Create a new student")]
    public async Task<CreateStudentPayload> CreateStudent(
        CreateStudentInput input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        var result = await mediator.Send(new CreateStudentCommand(input.Name, input.Email), ct);
        return new CreateStudentPayload(result);
    }

    [GraphQLDescription("Update an existing student")]
    public async Task<UpdateStudentPayload> UpdateStudent(
        int id,
        UpdateStudentInput input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        var result = await mediator.Send(new UpdateStudentCommand(id, input.Name, input.Email), ct);
        return new UpdateStudentPayload(result);
    }

    [GraphQLDescription("Delete a student")]
    public async Task<DeleteStudentPayload> DeleteStudent(
        int id,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        var success = await mediator.Send(new DeleteStudentCommand(id), ct);
        return new DeleteStudentPayload(success);
    }

    [GraphQLDescription("Enroll a student in a subject")]
    public async Task<EnrollStudentPayload> EnrollStudent(
        EnrollStudentInput input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        var result = await mediator.Send(new EnrollStudentCommand(input.StudentId, input.SubjectId), ct);
        return new EnrollStudentPayload(result);
    }

    [GraphQLDescription("Unenroll a student from a subject")]
    public async Task<UnenrollStudentPayload> UnenrollStudent(
        int enrollmentId,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        var success = await mediator.Send(new UnenrollStudentCommand(enrollmentId), ct);
        return new UnenrollStudentPayload(success);
    }
}

// Inputs
public record CreateStudentInput(string Name, string Email);
public record UpdateStudentInput(string Name, string Email);
public record EnrollStudentInput(int StudentId, int SubjectId);

// Payloads
public record CreateStudentPayload(StudentDto Student);
public record UpdateStudentPayload(StudentDto Student);
public record DeleteStudentPayload(bool Success);
public record EnrollStudentPayload(EnrollmentDto Enrollment);
public record UnenrollStudentPayload(bool Success);
