<?php

namespace App\Http\Requests;

use App\Core\Request;
use App\Exceptions\ValidationException;

class RegisterRequest
{
    public static function validate(Request $request): array
    {
        $data = $request->json();
        foreach (['name', 'email', 'password', 'phone', 'address'] as $field) {
            if (empty($data[$field])) {
                throw new ValidationException("Missing required field: {$field}", ['field' => $field]);
            }
        }

        if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
            throw new ValidationException('Invalid email format', ['field' => 'email']);
        }

        if (strlen((string)$data['password']) < 8) {
            throw new ValidationException('Password must be at least 8 characters', ['field' => 'password']);
        }

        return $data;
    }
}
