<?php
// /public_html/erp.jewellersbd.com/config/response.php

function send_response($status, $success, $message, $data = null) {
    http_response_code((int)$status);
    header('Content-Type: application/json; charset=UTF-8');

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

    exit;
}

function json_response($data, $status = 200) {
    http_response_code((int)$status);
    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

function jsonResponse($data, $status = 200) {
    http_response_code((int)$status);
    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

function csrf_token() {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }

    if (empty($_SESSION['csrf_token'])) {
        $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
    }

    return $_SESSION['csrf_token'];
}

function csrf_verify($token) {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }

    return isset($_SESSION['csrf_token']) &&
           hash_equals($_SESSION['csrf_token'], (string)$token);
}