using Application.Auth.Commands;
using Application.Auth.DTOs;
using MediatR;

namespace Adapters.Driving.Api.Types.Auth;

[ExtendObjectType(typeof(Mutation))]
public class AuthMutations
{
    /// <summary>
    /// Authenticates a user and returns a JWT token.
    /// </summary>
    public async Task<AuthResponse> Login(
        LoginRequest input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        return await mediator.Send(new LoginCommand(input.Username, input.Password), ct);
    }

    /// <summary>
    /// Registers a new user account. Optionally creates a student profile.
    /// Returns a recovery code that should be saved securely (shown only once).
    /// </summary>
    public async Task<AuthResponse> Register(
        RegisterRequest input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        return await mediator.Send(
            new RegisterCommand(input.Username, input.Password, input.Name, input.Email),
            ct
        );
    }

    /// <summary>
    /// Resets a user's password using their recovery code.
    /// </summary>
    [GraphQLDescription("Reset password using recovery code (no email required)")]
    public async Task<ResetPasswordResponse> ResetPassword(
        ResetPasswordRequest input,
        [Service] IMediator mediator,
        CancellationToken ct)
    {
        return await mediator.Send(
            new ResetPasswordCommand(input.Username, input.RecoveryCode, input.NewPassword),
            ct
        );
    }
}
