<?php
/**
 * Configuration Controller – GForce Module
 *
 * @category    PortalPeople
 * @package     GForce
 * @author      Portal People Development Team
 * @copyright   Copyright (c) 2026 Portal People (https://portalpeople.co.uk)
 */

namespace PortalPeople\gforce\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use PortalPeople\gforce\models\ConfigForm;

/**
 * ConfigController
 *
 * Provides a web UI for managing GForce module settings and testing
 * the API connection.
 */
class ConfigController extends Controller
{
    /** @inheritdoc */
    public function behaviors(): array
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class'   => VerbFilter::class,
                'actions' => [
                    'save'            => ['POST'],
                    'test-connection' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Display and process the configuration form.
     *
     * @return string
     */
    public function actionIndex()
    {
        $model = new ConfigForm();
        $model->loadFromParams();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->saveToParams()) {
                Yii::$app->session->setFlash('success', 'GForce configuration saved successfully.');
                return $this->refresh();
            }
            Yii::$app->session->setFlash('error', 'Failed to save configuration. Check file permissions.');
        }

        return $this->render('index', ['model' => $model]);
    }

    /**
     * AJAX: test API connection with the submitted form values.
     *
     * @return array
     */
    public function actionTestConnection(): array
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $model = new ConfigForm();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $result = $model->testConnection();
            return [
                'success' => $result['success'],
                'message' => $result['message'],
            ];
        }

        return [
            'success' => false,
            'message' => 'Invalid configuration data.',
            'errors'  => $model->errors,
        ];
    }

    /**
     * AJAX: return current module configuration status.
     *
     * @return array
     */
    public function actionStatus(): array
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        /** @var \PortalPeople\gforce\Module $module */
        $module = Yii::$app->getModule('gforce');

        return [
            'success'    => true,
            'configured' => $module ? $module->isConfigured() : false,
        ];
    }
}
