<?php

declare(strict_types=1);

namespace PortalPeople\datadash\controllers\api;

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

/**
 * @OA\Tag(
 *   name="DataDash",
 *   description="Datafeeds dashboard: order uploads and CSV exports"
 * )
 */
class DataController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors(): array
    {
        return array_merge(parent::behaviors(), [
            'verbs' => [
                'class'   => VerbFilter::class,
                'actions' => [
                    'upload'             => ['POST'],
                    'downloadorders'     => ['GET'],
                    'downloadpaidorders' => ['GET'],
                    'downloadcsv'        => ['GET'],
                    'downloadpaidcsv'    => ['GET'],
                    'downloadcsvgroup'   => ['GET'],
                    'downloaded'         => ['POST'],
                ],
            ],
        ]);
    }

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

    /**
     * @OA\Post(path="/df/data/upload", tags={"DataDash"})
     */
    public function actionUpload(): mixed
    {
        $p = Yii::$app->request->getBodyParams();

        return $this->module()->getDataService()->upload(
            $p['model']      ?? '',
            $p['connection'] ?? '',
            $p['database']   ?? '',
            $p['company']    ?? ''
        );
    }

    /**
     * @OA\Get(path="/df/data/downloadorders", tags={"DataDash"})
     */
    public function actionDownloadorders(): array
    {
        return $this->module()->getDataService()->downloadOrders(
            Yii::$app->request->getQueryParam('type', 'json')
        );
    }

    /**
     * @OA\Get(path="/df/data/downloadpaidorders", tags={"DataDash"})
     */
    public function actionDownloadpaidorders(): array
    {
        return $this->module()->getDataService()->downloadPaidOrders(
            Yii::$app->request->getQueryParam('type', 'json')
        );
    }

    /**
     * @OA\Get(path="/df/data/downloadcsv", tags={"DataDash"})
     */
    public function actionDownloadcsv(): mixed
    {
        return $this->module()->getDataService()->downloadCsv(
            Yii::$app->request->getQueryParam('orderref')
        );
    }

    /**
     * @OA\Get(path="/df/data/downloadpaidcsv", tags={"DataDash"})
     */
    public function actionDownloadpaidcsv(): mixed
    {
        return $this->module()->getDataService()->downloadPaidCsv(
            Yii::$app->request->getQueryParam('orderref')
        );
    }

    /**
     * @OA\Get(path="/df/data/downloadcsvgroup", tags={"DataDash"})
     */
    public function actionDownloadcsvgroup(): mixed
    {
        return $this->module()->getDataService()->downloadCsvGroup(
            Yii::$app->request->getQueryParam('month')
        );
    }

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