<?php

namespace App\Http\Controllers;

use App\Core\Request;
use App\Core\Response;
use App\Domain\Services\PasswordResetService;
use App\Exceptions\ValidationException;
use App\Http\Responses\ApiResponse;

class PasswordController
{
    public function __construct(private PasswordResetService $passwords)
    {
    }

    public function forgotPassword(Request $request): Response
    {
        $email = $request->input('email');
        if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new ValidationException('Valid email is required', ['field' => 'email']);
        }

        $token = $this->passwords->create($email);
        return ApiResponse::success(['reset_token' => $token], 202, 'Reset instructions queued');
    }

    public function resetPassword(Request $request): Response
    {
        $token = $request->input('token');
        $password = $request->input('password');
        if (!$token || !$password || strlen($password) < 8) {
            throw new ValidationException('Token and password of at least 8 characters are required');
        }

        $this->passwords->reset($token, $password);
        return ApiResponse::success(null, 200, 'Password reset successful');
    }
}
