<?php

declare(strict_types=1);

namespace PortalPeople\dataexchange\controllers\api;

use OpenApi\Annotations as OA;
use PortalPeople\dataexchange\Module;
use Yii;
use yii\rest\Controller;
use yii\filters\VerbFilter;

/**
 * @OA\Tag(
 *   name="DataExchange",
 *   description="ERP data import/export and order download endpoints"
 * )
 */
class DataController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors(): array
    {
        return array_merge(parent::behaviors(), [
            'verbs' => [
                'class'   => VerbFilter::class,
                'actions' => [
                    'index'               => ['GET'],
                    'model'               => ['GET'],
                    'upload'              => ['POST'],
                    'downloadorders'      => ['GET'],
                    'downloadpaidorders'  => ['GET'],
                    'downloadcsv'         => ['GET'],
                    'downloadpaidcsv'     => ['GET'],
                    'downloadcsvgroup'    => ['GET'],
                    'downloaded'          => ['POST'],
                    'download'            => ['GET'],
                    'check'               => ['GET', 'OPTIONS'],
                    'checkids'            => ['POST'],
                    'sagedata'            => ['POST'],
                ],
            ],
        ]);
    }

    /** @return \PortalPeople\dataexchange\Module */
    private function module(): Module
    {
        return Yii::$app->getModule('ngauge-data-exchange');
    }

    // -------------------------------------------------------------------------
    // Actions
    // -------------------------------------------------------------------------

    /**
     * @OA\Get(path="/v1/data/index", tags={"DataExchange"})
     */
    public function actionIndex(): array
    {
        $model = Yii::$app->request->getQueryParam('model');
        $days  = (int)Yii::$app->request->getQueryParam('days', 30);
        $qty   = (int)Yii::$app->request->getQueryParam('qty', 100);
        $order = Yii::$app->request->getQueryParam('order', 'id');

        return $this->module()->getDataExchangeService()->index($model, $days, $qty, $order);
    }

    /**
     * @OA\Get(path="/v1/data/model", tags={"DataExchange"})
     */
    public function actionModel(): array
    {
        $model = Yii::$app->request->getQueryParam('model');

        return $this->module()->getDataExchangeService()->getModelFields($model);
    }

    /**
     * @OA\Post(path="/v1/data/upload", tags={"DataExchange"})
     */
    public function actionUpload(): mixed
    {
        $params     = Yii::$app->request->getBodyParams();
        $modelname  = $params['model']      ?? '';
        $connection = $params['connection'] ?? '';
        $db         = $params['db']         ?? '';
        $company    = $params['company']    ?? '';

        return $this->module()->getDataExchangeService()->upload($modelname, $connection, $db, $company);
    }

    /**
     * @OA\Get(path="/v1/data/downloadorders", tags={"DataExchange"})
     */
    public function actionDownloadorders(): array
    {
        $type = Yii::$app->request->getQueryParam('type', 'json');

        return $this->module()->getDataExchangeService()->downloadOrders($type);
    }

    /**
     * @OA\Get(path="/v1/data/downloadpaidorders", tags={"DataExchange"})
     */
    public function actionDownloadpaidorders(): array
    {
        $type = Yii::$app->request->getQueryParam('type', 'json');

        return $this->module()->getDataExchangeService()->downloadPaidOrders($type);
    }

    /**
     * @OA\Get(path="/v1/data/downloadcsv", tags={"DataExchange"})
     */
    public function actionDownloadcsv(): mixed
    {
        $orderref = Yii::$app->request->getQueryParam('orderref');

        return $this->module()->getDataExchangeService()->downloadCsv($orderref);
    }

    /**
     * @OA\Get(path="/v1/data/downloadpaidcsv", tags={"DataExchange"})
     */
    public function actionDownloadpaidcsv(): mixed
    {
        $orderref = Yii::$app->request->getQueryParam('orderref');

        return $this->module()->getDataExchangeService()->downloadPaidCsv($orderref);
    }

    /**
     * @OA\Get(path="/v1/data/downloadcsvgroup", tags={"DataExchange"})
     */
    public function actionDownloadcsvgroup(): mixed
    {
        $month = Yii::$app->request->getQueryParam('month');

        return $this->module()->getDataExchangeService()->downloadCsvGroup($month);
    }

    /**
     * @OA\Post(path="/v1/data/downloaded", tags={"DataExchange"})
     */
    public function actionDownloaded(): mixed
    {
        return $this->module()->getDataExchangeService()->markDownloaded(
            Yii::$app->request->getBodyParams()
        );
    }

    /**
     * @OA\Get(path="/v1/data/download", tags={"DataExchange"})
     */
    public function actionDownload(): string
    {
        $date = Yii::$app->request->getQueryParam('date');

        return $this->module()->getDataExchangeService()->download($date);
    }

    /**
     * @OA\Get(path="/v1/data/check", tags={"DataExchange"})
     */
    public function actionCheck(): bool
    {
        return $this->module()->getDataExchangeService()->check();
    }

    /**
     * @OA\Post(path="/v1/data/checkids", tags={"DataExchange"})
     */
    public function actionCheckids(): array
    {
        $modelname = Yii::$app->request->getBodyParam('model');

        return $this->module()->getDataExchangeService()->checkIds($modelname);
    }

    /**
     * @OA\Post(path="/v1/data/sagedata", tags={"DataExchange"})
     */
    public function actionSagedata(): void
    {
        $data = Yii::$app->request->getBodyParam('data', []);

        $this->module()->getDataExchangeService()->importSageData($data);
    }
}
