<?php

declare(strict_types=1);

namespace PortalPeople\datadash;

use PortalPeople\datadash\services\DataService;
use PortalPeople\datadash\services\DataServicesService;
use yii\base\BootstrapInterface;
use yii\base\Module as BaseModule;
use Yii;

/**
 * ngauge-datadash module
 *
 * Datafeeds dashboard — provides order download, CSV export, and Tour Operator
 * data feed endpoints (SLT, SOH, SOD, STA, SLA).
 */
class Module extends BaseModule implements BootstrapInterface
{
    public string $moduleId = 'ngauge-datadash';
    public bool   $enabled  = true;

    /** @var DataService|null */
    private ?DataService $dataService = null;

    /** @var DataServicesService|null */
    private ?DataServicesService $dataServicesService = null;

    public $controllerNamespace = 'PortalPeople\\datadash\\controllers';

    public function init(): void
    {
        parent::init();

        $config = Yii::$app->params['plugins'][$this->moduleId] ?? [];

        if (isset($config['enabled'])) {
            $this->enabled = (bool)$config['enabled'];
        }
    }

    public function bootstrap($app): void
    {
        $app->setModule($this->moduleId, $this);

        if ($app instanceof \yii\web\Application) {
            $app->getUrlManager()->addRules([
                'POST df/data/upload'            => 'ngauge-datadash/api/data/upload',
                'GET df/data/downloadorders'     => 'ngauge-datadash/api/data/downloadorders',
                'GET df/data/downloadpaidorders' => 'ngauge-datadash/api/data/downloadpaidorders',
                'GET df/data/downloadcsv'        => 'ngauge-datadash/api/data/downloadcsv',
                'GET df/data/downloadpaidcsv'    => 'ngauge-datadash/api/data/downloadpaidcsv',
                'GET df/data/downloadcsvgroup'   => 'ngauge-datadash/api/data/downloadcsvgroup',
                'POST df/data/downloaded'        => 'ngauge-datadash/api/data/downloaded',
                'GET df/data-services/slt'       => 'ngauge-datadash/api/data-services/slt',
                'GET df/data-services/soh'       => 'ngauge-datadash/api/data-services/soh',
                'GET df/data-services/sod'       => 'ngauge-datadash/api/data-services/sod',
                'GET df/data-services/sta'       => 'ngauge-datadash/api/data-services/sta',
                'GET df/data-services/sla'       => 'ngauge-datadash/api/data-services/sla',
            ], false);
        }
    }

    public function getDataService(): DataService
    {
        if ($this->dataService === null) {
            $this->dataService = new DataService();
        }

        return $this->dataService;
    }

    public function getDataServicesService(): DataServicesService
    {
        if ($this->dataServicesService === null) {
            $this->dataServicesService = new DataServicesService();
        }

        return $this->dataServicesService;
    }
}
