<?php
namespace App\Core;

class Response {
    public function setStatusCode($code) {
        http_response_code($code);
    }

    public function redirect($url) {
        header("Location: $url");
        exit;
    }

    public function json(array $data, $statusCode = 200) {
        $this->setStatusCode($statusCode);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;
    }

    public function render($view, $params = []) {
        // Simple view renderer
        // Views will extract the parameters into variables
        extract($params);
        
        // Define a helper for translations inside views
        $t = function($key) {
            return \App\Services\TranslationService::t($key);
        };
        
        // Define a helper for dynamic base URL inside views
        $baseUrl = (isset($GLOBALS['router'])) ? $GLOBALS['router']->getBaseUrl() : '';
        
        $viewFile = __DIR__ . "/../Views/{$view}.php";
        if (file_exists($viewFile)) {
            include $viewFile;
        } else {
            header("HTTP/1.0 500 Internal Server Error");
            echo "<h1>Template $view not found</h1>";
            exit;
        }
    }
}
