<?php 
    namespace Core;
    use Core\View;
    class Response {

        public const OK = 200;
        public const CREATED = 201;
        public const ACCEPTED = 202;

        public const NO_CONTENT = 204;

        public const BAD_REQUEST = 400;
        public const UNAUTHORIZED = 401;
        public const FORBIDDEN = 403;
        public const NOT_FOUND = 404;
        public const METHOD_NOT_ALLOWED = 405;

        public const INTERNAL_SERVER_ERROR = 500;
        public const NOT_IMPLEMENTED = 501;
        public const BAD_GATEWAY = 502;
        public const SERVICE_UNAVAILABLE = 503;
        public static function json($data, $statusCode = 200) {
            http_response_code($statusCode);
            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }

        public static function text($string, $statusCode = 200) {
            http_response_code($statusCode);
            header('Content-Type: text/plain');
            echo $string;
            exit;
        }

        public static function html($html, $statusCode = 200) {
            http_response_code($statusCode);
            header('Content-Type: text/html');
            echo $html;
            exit;
        }

        public static function redirect($url, $statusCode = 302) {
            http_response_code($statusCode);
            header('Location: ' . APP_URL . $url);
            exit;
        }

        public static function withHeaders($headers = []) {
            foreach ($headers as $key => $value) {
                header("$key: $value");
            }
            return new self();
        }

        public static function back(array $params = [], $statusCode = 302) {
            $url = $_SERVER['HTTP_REFERER'] ?? APP_URL;

            if (!empty($params)) {
                $queryString = http_build_query($params);
                $separator = (str_contains($url, '?')) ? '&' : '?';
                $url .= $separator . $queryString;
            }

            http_response_code($statusCode);
            header("Location: $url");
            exit;
        }

        public static function abort(int $code, string $title = '', string $content = ''): void
        {
            // This is a CRITICAL step. It sends the HTTP status header
            // to the browser before any content is rendered.
            http_response_code($code);

            // The data is passed to the view inside an array, as expected by
            // your View::with() method.
            $data = [
                'code' => $code,
                'title' => $title, 
                'content' => $content
            ];
            
            switch ($code) {
                case 404:
                    // Use the View::make() method to render the 404 view with the data.
                    View::make('error/404')->with($data)->setLayout('landingLayout')->dontMinify()->render();
                    break;
                case 403:
                    // Use the View::make() method to render the 403 view with the data.
                    View::make('error/403')->with($data)->setLayout('landingLayout')->render();
                    break;
                case 500:
                    View::make('error/500')->with($data)->setLayout('landingLayout')->render();
                    break;
                default:
                    // Use the View::make() method to render the general error view with the data.
                    View::make('error/500')->with($data)->setLayout('landingLayout')->render();
                    break;
            }

            // The `exit` statement ensures that no other code, including
            // your main `View::make()` call, is executed after the error
            // page has been rendered.
            exit;
        }
    }
?>