<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_Administrator_Component
 *
 * @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\Component\MefisAssetBridge\Administrator\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Mefi\Component\MefisAssetBridge\Administrator\Traits\CoreBridgeTrait;

/**
 * Auth Controller
 *
 * @since  1.0.0
 */
class ApikeyController extends BaseController
{
    use CoreBridgeTrait;
    /**
     * Authentication helper
     *
     * @var    AuthHelper
     * @since  1.0.0
     */
    protected $authHelper;

    /**
     * Constructor.
     *
     * @param   array                $config   An optional associative array of configuration settings.
     * @param   MVCFactoryInterface  $factory  The factory.
     * @param   CMSApplication       $app      The JApplication for the dispatcher
     * @param   Input                $input    Input
     *
     * @since   1.0.0
     */
    public function __construct($config = [], $factory = null, $app = null, $input = null)
    {
        parent::__construct($config, $factory, $app, $input);

        if(!$this->isAppRegistered()){
            $this->setRedirect(Route::_('index.php?option=com_mefisassetbridge&view=problems', false), Text::_('COM_MEFISASSETBRIDGE_APP_NOT_REGISTERED'), 'error');
            return;
        }
    }

    /**
     * Logout from the system
     *
     * @return  void
     *
     * @since   1.0.0
     */
    public function delete()
    {
        // $app = $this->app;
        if (! $this->isAuthenticated()) {
            // Redirect to the auth view if not authenticated
            $this->setRedirect(Route::_('index.php?option=com_mefisassetbridge&view=auth'), Text::_('COM_MEFISASSETBRIDGE_AUTH_REQUIRED'));
            return;
        }
        $userDto = $this->getDataFromSession();

        if (!$userDto || !$userDto->getToken()) {
            $this->setRedirect(Route::_('index.php?option=com_mefisassetbridge&view=auth'), Text::_('COM_MEFISASSETBRIDGE_NOT_AUTHENTICATED'));
            return;
        }

        $apiKeyId = $this->input->getString('id', false);
        if(!$apiKeyId) {
            $this->setRedirect(Route::_('index.php?option=com_mefisassetbridge&view=apikeys'), Text::_('COM_MEFISASSETBRIDGE_MISSING_TOKEN_ID'));
            return;
        }

        $refreshToken = $userDto->getToken()->getRefreshToken();

        try {
            // Use the trait method to perform logout
            $this->coreDeleteApiKey($refreshToken, $apiKeyId);
            $message = Text::_('COM_MEFISASSETBRIDGE_API_KEY_DELETE_SUCCESS');
        } catch (\Exception $e) {
            $message = $e->getMessage() ?: Text::_('COM_MEFISASSETBRIDGE_API_KEY_DELETE_FAILED');
        }

        $this->setRedirect(Route::_('index.php?option=com_mefisassetbridge&view=apikeys'), $message);
        return;
    }
}
