<?php
namespace App\Core;

class Request {
    
    /**
     * Get request method
     */
    public function getMethod() {
        return strtoupper($_SERVER['REQUEST_METHOD']);
    }
    
    /**
     * Check if request is POST
     */
    public function isPost() {
        return $this->getMethod() === 'POST';
    }
    
    /**
     * Check if request is GET
     */
    public function isGet() {
        return $this->getMethod() === 'GET';
    }
    
    /**
     * Check if request is PUT
     */
    public function isPut() {
        return $this->getMethod() === 'PUT';
    }
    
    /**
     * Check if request is DELETE
     */
    public function isDelete() {
        return $this->getMethod() === 'DELETE';
    }
    
    /**
     * Check if request is AJAX
     */
    public function isAjax() {
        return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
               strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
    }
    
    /**
     * Get all request data
     */
    public function all() {
        $data = [];
        
        if ($this->isPost()) {
            $data = $_POST;
        } else {
            $data = $_GET;
        }
        
        // Get JSON input for API requests
        $input = json_decode(file_get_contents('php://input'), true);
        if ($input) {
            $data = array_merge($data, $input);
        }
        
        return $data;
    }
    
    /**
     * Get specific input
     */
    public function input($key, $default = null) {
        $data = $this->all();
        return $data[$key] ?? $default;
    }
    
    /**
     * Get POST data
     */
    public function post($key = null, $default = null) {
        if ($key === null) {
            return $_POST;
        }
        return $_POST[$key] ?? $default;
    }
    
    /**
     * Get GET data
     */
    public function get($key = null, $default = null) {
        if ($key === null) {
            return $_GET;
        }
        return $_GET[$key] ?? $default;
    }
    
    /**
     * Get uploaded file
     */
    public function file($key) {
        return $_FILES[$key] ?? null;
    }
    
    /**
     * Get request URI
     */
    public function getUri() {
        return $_SERVER['REQUEST_URI'];
    }
    
    /**
     * Get client IP address
     */
    public function getClientIp() {
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP']))
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_X_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        else if(isset($_SERVER['REMOTE_ADDR']))
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
    }
    
    /**
     * Get user agent
     */
    public function getUserAgent() {
        return $_SERVER['HTTP_USER_AGENT'] ?? '';
    }
    
    /**
     * Get request timestamp
     */
    public function getTimestamp() {
        return $_SERVER['REQUEST_TIME'];
    }
    
    /**
     * Validate required fields
     */
    public function validate($rules) {
        $data = $this->all();
        $errors = [];
        
        foreach ($rules as $field => $rule) {
            $value = $data[$field] ?? null;
            $rules = explode('|', $rule);
            
            foreach ($rules as $singleRule) {
                if ($singleRule === 'required' && empty($value)) {
                    $errors[$field][] = "$field is required";
                }
                
                if ($singleRule === 'email' && !empty($value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
                    $errors[$field][] = "$field must be a valid email";
                }
                
                if (strpos($singleRule, 'min:') === 0) {
                    $min = explode(':', $singleRule)[1];
                    if (strlen($value) < $min) {
                        $errors[$field][] = "$field must be at least $min characters";
                    }
                }
                
                if (strpos($singleRule, 'max:') === 0) {
                    $max = explode(':', $singleRule)[1];
                    if (strlen($value) > $max) {
                        $errors[$field][] = "$field must not exceed $max characters";
                    }
                }
            }
        }
        
        return $errors;
    }
}