<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_System_Plugin
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */
namespace Mefi\Plugin\System\MefisAssetBridge\Ajax;

defined('_JEXEC') or die;

use Joomla\CMS\Input\Input;
use MefisAssetBridgeCore\Api\ApiFactory;
use MefisAssetBridgeCore\Auth\AuthenticatedUser;

/**
 * Base class for AJAX handlers
 *
 * Provides common functionality for all handlers
 *
 * @since  1.0.0
 */
abstract class BaseHandler implements HandlerInterface
{


    /**
     * Create direct response - this will become the direct output for Joomla's com_ajax
     * Note that Joomla wraps this in an outer success/message/data structure
     *
     * @param   bool    $success  Success status
     * @param   string  $message  Response message
     * @param   mixed   $data     Response data
     *
     * @return  array   Response array that will be converted to JSON
     */
    protected function createResponse(bool $success, string $message = '', $data = null): array
    {
        if($message === '' && !empty($data['message'])) {
            $message = $data['message'];
        }

        return [
            'success' => $success,
            'message' => $message,
            'data' => $data
        ];
    }

    /**
     * Create success response
     *
     * @param   string  $message  Success message
     * @param   mixed   $data     Response data
     *
     * @return  array   Formatted response
     */
    protected function createSuccessResponse(string $message = '', $data = null): array
    {
        return $this->createResponse(true, $message, $data);
    }

    /**
     * Create error response
     *
     * @param   string  $message  Error message
     * @param   mixed   $data     Optional error details
     *
     * @return  array   Formatted error response
     */
    protected function createErrorResponse(string $message = 'Unknown error', $data = null): array
    {
        return $this->createResponse(false, $message, $data);
    }

}