<?php
/**
 * Response Builder
 *
 * @package ZenoChatbot\Framework\Http
 */

namespace ZenoChatbot\Framework\Http;

use ZenoChatbot\Framework\Foundation\Application;

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

/**
 * Fluent Response Builder
 */
class ResponseBuilder {

    /**
     * Application instance
     *
     * @var Application
     */
    private $app;

    /**
     * Response data
     *
     * @var mixed
     */
    private $data;

    /**
     * Constructor
     *
     * @param Application $app
     * @param mixed $data
     */
    public function __construct(Application $app, $data = []) {
        $this->app = $app;
        $this->data = $data;
    }

    /**
     * Create a success response
     *
     * @param mixed $data
     * @param string|null $message
     * @param int $statusCode
     * @return mixed
     */
    public function success($data = null, $message = null, $statusCode = 200) {
        $responseData = $data !== null ? $data : $this->data;
        $response = Response::success($responseData, $message, $statusCode);
        
        return $this->sendResponse($response);
    }

    /**
     * Create an error response
     *
     * @param string $message
     * @param mixed $data
     * @param int $statusCode
     * @return mixed
     */
    public function error($message, $data = null, $statusCode = 400) {
        $responseData = $data !== null ? $data : $this->data;
        $response = Response::error($message, $responseData, $statusCode);
        
        return $this->sendResponse($response);
    }

    /**
     * Create a "created" success response
     *
     * @param mixed $data
     * @param string $message
     * @return mixed
     */
    public function created($data = null, $message = 'Resource created successfully') {
        $responseData = $data !== null ? $data : $this->data;
        $response = Response::created($responseData, $message);
        
        return $this->sendResponse($response);
    }

    /**
     * Create a "not found" error response
     *
     * @param string $message
     * @return mixed
     */
    public function notFound($message = 'Resource not found') {
        $response = Response::notFound($message);
        
        return $this->sendResponse($response);
    }

    /**
     * Create an "unauthorized" error response
     *
     * @param string $message
     * @return mixed
     */
    public function unauthorized($message = 'Unauthorized access') {
        $response = Response::unauthorized($message);
        
        return $this->sendResponse($response);
    }

    /**
     * Create a "forbidden" error response
     *
     * @param string $message
     * @return mixed
     */
    public function forbidden($message = 'Access forbidden') {
        $response = Response::forbidden($message);
        
        return $this->sendResponse($response);
    }

    /**
     * Create a "validation error" response
     *
     * @param array $errors
     * @param string $message
     * @return mixed
     */
    public function validationError($errors = [], $message = 'Validation failed') {
        $response = Response::validationError($errors, $message);
        
        return $this->sendResponse($response);
    }

    /**
     * Create an "internal server error" response
     *
     * @param string $message
     * @param mixed $debug
     * @return mixed
     */
    public function serverError($message = 'Internal server error', $debug = null) {
        $response = Response::serverError($message, $debug);
        
        return $this->sendResponse($response);
    }

    /**
     * Send the response based on request type
     *
     * @param Response $response
     * @return mixed
     */
    private function sendResponse(Response $response) {
        $requestType = $this->app->getRequestType();
        
        if ($requestType === 'rest') {
            return $response->sendRest();
        } else {
            // For AJAX requests, send and exit
            $response->sendAjax();
            return null; // This won't be reached due to wp_send_json_* exit behavior
        }
    }
}
