<?php
namespace Core;

class Request {
    // --- Sanitized methods (safe for HTML output) ---
    public static function get($key, $default = null) {
        return isset($_GET[$key]) ? htmlspecialchars($_GET[$key], ENT_QUOTES, 'UTF-8') : $default;
    }

    public static function post($key, $default = null) {
        return isset($_POST[$key]) ? htmlspecialchars($_POST[$key], ENT_QUOTES, 'UTF-8') : $default;
    }

    // --- FILE UPLOAD METHODS ---
    public static function file($key, $default = null) {
        return isset($_FILES[$key]) ? $_FILES[$key] : $default;
    }

    public static function files() {
        return $_FILES;
    }

    public static function hasFile($key) {
        return isset($_FILES[$key]) && $_FILES[$key]['error'] === UPLOAD_ERR_OK;
    }

    public static function input($key, $default = null) {
        $data = array_merge($_GET, $_POST);
        return isset($data[$key]) ? htmlspecialchars($data[$key], ENT_QUOTES, 'UTF-8') : $default;
    }

    public static function all() {
        return array_map(fn($value) => htmlspecialchars($value, ENT_QUOTES, 'UTF-8'), array_merge($_GET, $_POST));
    }

    // --- Raw methods (no sanitization) ---
    public static function rawGet($key, $default = null) {
        return $_GET[$key] ?? $default;
    }

    public static function rawPost($key, $default = null) {
        return $_POST[$key] ?? $default;
    }

    public static function rawInput($key, $default = null) {
        $data = array_merge($_GET, $_POST);
        return $data[$key] ?? $default;
    }

    public static function rawAll() {
        return array_merge($_GET, $_POST);
    }

    // --- JSON stays as-is (raw) ---
    public static function json() {
        $raw = file_get_contents('php://input');
        $decoded = json_decode($raw, true);
        return is_array($decoded) ? $decoded : [];
    }
}
