<?php
/**
 * App Facade
 *
 * @package ZenoChatbot
 */

namespace ZenoChatbot;

use ZenoChatbot\Framework\Foundation\App as BaseApp;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * App facade for accessing application instance
 *
 * @method static \ZenoChatbot\Framework\Foundation\Application getInstance()
 * @method static mixed make(string $abstract)
 * @method static \ZenoChatbot\Framework\Database\QueryBuilder db()
 * @method static mixed config(string $key, mixed $default = null)
 * @method static string basePath(string $path = '')
 * @method static string resourcePath(string $path = '')
 * @method static string assetPath(string $path = '')
 * @method static bool isDebug()
 * @method static string environment()
 * @method static bool isEnvironment(string|array $environments)
 * @method static void bind(string $abstract, mixed $concrete = null)
 * @method static void singleton(string $abstract, mixed $concrete = null)
 * @method static bool bound(string $abstract)
 */
class App extends BaseApp
{
    /**
     * Get request instance
     *
     * @return \ZenoChatbot\Framework\Http\Request
     */
    public static function request()
    {
        return static::make('request');
    }

    /**
     * Get current page number for pagination
     *
     * @param string $pageName
     * @return int
     */
    public static function page($pageName = 'page')
    {
        return static::request()->page($pageName);
    }

    /**
     * Get per page value for pagination
     *
     * @param int $default
     * @param int $max
     * @return int
     */
    public static function perPage($default = 15, $max = 100)
    {
        return static::request()->perPage($default, $max);
    }

    /**
     * Create a paginator instance
     *
     * @param mixed $items
     * @param int $total
     * @param int|null $perPage
     * @param int|null $currentPage
     * @param array $options
     * @return \ZenoChatbot\Framework\Database\LengthAwarePaginator
     */
    public static function paginate($items, $total, $perPage = null, $currentPage = null, array $options = [])
    {
        $perPage = $perPage ?: static::perPage();
        $currentPage = $currentPage ?: static::page();

        return new \ZenoChatbot\Framework\Database\LengthAwarePaginator(
            $items, $total, $perPage, $currentPage, $options
        );
    }

    // Configuration helper removed - using static values now

    /**
     * Quick access to common services
     */

    /**
     * Get LLM service
     *
     * @return \ZenoChatbot\Services\LLMService
     */
    public static function llm()
    {
        return static::make(\ZenoChatbot\Services\LLMService::class);
    }

    /**
     * Get Index service
     *
     * @return \ZenoChatbot\Services\IndexService
     */
    public static function index()
    {
        return static::make(\ZenoChatbot\Services\IndexService::class);
    }

    /**
     * Get Chat Logger service
     *
     * @return \ZenoChatbot\Services\ChatLoggerService
     */
    public static function chatLogger()
    {
        return static::make(\ZenoChatbot\Services\ChatLoggerService::class);
    }

    /**
     * Create REST response
     *
     * @param mixed $data
     * @param int $status
     * @param array $headers
     * @return \WP_REST_Response
     */
    public static function restResponse($data = null, $status = 200, array $headers = [])
    {
        $response = new \WP_REST_Response($data, $status);

        foreach ($headers as $key => $value) {
            $response->header($key, $value);
        }

        return $response;
    }

    /**
     * Create paginated REST response
     *
     * @param \ZenoChatbot\Framework\Database\LengthAwarePaginator $paginator
     * @param int $status
     * @param array $headers
     * @return \WP_REST_Response
     */
    public static function restPaginatedResponse($paginator, $status = 200, array $headers = [])
    {
        $response = static::restResponse($paginator->toRestResponse(), $status, $headers);

        // Add pagination headers
        $response->header('X-Total-Count', $paginator->total());
        $response->header('X-Per-Page', $paginator->perPage());
        $response->header('X-Current-Page', $paginator->currentPage());
        $response->header('X-Last-Page', $paginator->lastPage());

        if ($paginator->hasMorePages()) {
            $response->header('X-Has-More-Pages', 'true');
        }

        return $response;
    }

    /**
     * Log message
     *
     * @param string $message
     * @param string $level
     * @return void
     */
    public static function log($message, $level = 'info')
    {
        if (static::isDebug()) {

        }
    }
}
