<?php

namespace App\Core;

/**
 * API Response Helper - مساعد لاستجابات API موحدة
 */
class ApiResponse
{
    /**
     * استجابة نجاح
     */
    public static function success($data = null, string $message = '', int $statusCode = 200): void
    {
        $response = [
            'success' => true,
            'message' => $message,
            'data' => $data
        ];

        http_response_code($statusCode);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    /**
     * استجابة خطأ
     */
    public static function error(string $message, int $statusCode = 400, $errors = null): void
    {
        $response = [
            'success' => false,
            'error' => $message,
            'data' => null
        ];

        if ($errors !== null) {
            $response['errors'] = $errors;
        }

        http_response_code($statusCode);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    /**
     * استجابة غير مصرح
     */
    public static function unauthorized(string $message = 'غير مصرح'): void
    {
        self::error($message, 401);
    }

    /**
     * استجابة محظور
     */
    public static function forbidden(string $message = 'غير مسموح'): void
    {
        self::error($message, 403);
    }

    /**
     * استجابة غير موجود
     */
    public static function notFound(string $message = 'غير موجود'): void
    {
        self::error($message, 404);
    }

    /**
     * استجابة Rate Limit
     */
    public static function rateLimitExceeded(string $message = 'تم تجاوز حد الطلبات'): void
    {
        self::error($message, 429);
    }

    /**
     * استجابة خطأ خادم
     */
    public static function serverError(string $message = 'خطأ في الخادم', bool $debug = false, \Throwable $e = null): void
    {
        $response = [
            'success' => false,
            'error' => $message,
            'data' => null
        ];

        if ($debug && $e) {
            $response['debug'] = [
                'message' => $e->getMessage(),
                'file' => $e->getFile(),
                'line' => $e->getLine()
            ];
        }

        http_response_code(500);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    /**
     * استجابة paginated
     */
    public static function paginated(array $data, int $total, int $page, int $perPage, string $message = ''): void
    {
        $response = [
            'success' => true,
            'message' => $message,
            'data' => $data,
            'pagination' => [
                'total' => $total,
                'page' => $page,
                'per_page' => $perPage,
                'total_pages' => (int)ceil($total / $perPage),
                'has_next' => ($page * $perPage) < $total,
                'has_prev' => $page > 1
            ]
        ];

        http_response_code(200);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }
}
