<?php

namespace App\Helpers;

class Response
{

    /**
     * Send response
     * 
     * @throws Exception
     * 
     * @return void
     * 
     * @param bool $success
     * @param string $message
     * @param int $code
     * @param array $data
     */
    private static function send(
        bool $success,
        string $message,
        int $code,
        array $data = []
    ): void {

        if (!headers_sent()) {
            http_response_code($code);
            header('Content-Type: application/json; charset=utf-8');
        }

        echo json_encode([
            'success' => $success,
            'code'    => $code,
            'message' => $message,
            'data'    => $data
        ], JSON_UNESCAPED_UNICODE);

        exit;
    }

    /**
     * Send success response
     * 
     * @return void
     * 
     * @param string $message
     * @param array $data
     * @param int $code
     * 
     * @throws Exception
     */
    public static function success(
        string $message = 'OK',
        array $data = [],
        int $code = 200
    ): void {
        self::send(true, $message, $code, $data);
    }

    /**
     *  Send error response
     *  
     * @return void
     * 
     * @param string $message
     * @param int $code
     * @param array $data
     */
    public static function error(
        string $message = 'Error',
        int $code = 400,
        array $data = []
    ): void {
        self::send(false, $message, $code, $data);
    }
}
