<?php
namespace CAE\Lib;

class Response
{
    public static function json(array $data, int $code = 200): void
    {
        http_response_code($code);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    public static function success(mixed $data = null, string $message = 'OK', int $code = 200): void
    {
        self::json(['success' => true, 'message' => $message, 'data' => $data], $code);
    }

    public static function error(string $message, int $code = 400, array $errors = []): void
    {
        self::json(['success' => false, 'message' => $message, 'errors' => $errors], $code);
    }

    public static function redirect(string $url): void
    {
        header('Location: ' . BASE_URL . $url);
        exit;
    }

    public static function download(string $content, string $filename, string $mimeType): void
    {
        header('Content-Type: ' . $mimeType);
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Length: ' . strlen($content));
        echo $content;
        exit;
    }
}
