<?php

declare(strict_types=1);

namespace PortalPeople\datadash\services;

use Yii;
use yii\rest\Serializer;

/**
 * DataServicesService
 *
 * Tour Operator data feeds: SLT, SOH, SOD, STA, SLA.
 * All actions are gated behind Tour Operator group membership.
 * Extracted from api/modules/df/controllers/DataServicesController.
 */
class DataServicesService
{
    /** @var array<string, string> Allowed identity groups */
    private const ALLOWED_GROUPS = ['Tour Operator', 'Tour Operator Staff', 'Global Admins'];

    /**
     * Serializer instance shared across service calls.
     */
    private function serializer(): Serializer
    {
        return new Serializer(['collectionEnvelope' => 'items']);
    }

    /**
     * Check whether the current user belongs to an allowed group.
     */
    private function isAllowed(): bool
    {
        /** @var \backend\models\User $user */
        $user = Yii::$app->user->identity;

        foreach (self::ALLOWED_GROUPS as $group) {
            if ($user->inGroup($group)) {
                return true;
            }
        }

        return false;
    }

    /**
     * SLT (Sales Lead Time) data feed.
     *
     * @param array<string, mixed> $queryParams
     * @return array<string, mixed>|array<never, never>
     */
    public function slt(array $queryParams): array
    {
        if (!$this->isAllowed()) {
            return [];
        }

        $searchModel  = new \PortalPeople\datadash\models\SltSearch();
        $dataProvider = $searchModel->search($queryParams);

        return $this->serializer()->serialize($dataProvider);
    }

    /**
     * SOH (Stock on Hand) data feed.
     *
     * @param array<string, mixed> $queryParams
     * @return array<string, mixed>|array<never, never>
     */
    public function soh(array $queryParams): array
    {
        if (!$this->isAllowed()) {
            return [];
        }

        $searchModel  = new \PortalPeople\datadash\models\SohSearch();
        $dataProvider = $searchModel->search($queryParams);

        return $this->serializer()->serialize($dataProvider);
    }

    /**
     * SOD (Stock on Demand) data feed.
     *
     * @param array<string, mixed> $queryParams
     * @return array<string, mixed>|array<never, never>
     */
    public function sod(array $queryParams): array
    {
        if (!$this->isAllowed()) {
            return [];
        }

        // Note: original used SohSearch for SOD as well — preserved faithfully
        $searchModel  = new \PortalPeople\datadash\models\SohSearch();
        $dataProvider = $searchModel->search($queryParams);

        return $this->serializer()->serialize($dataProvider);
    }

    /**
     * STA (Stock Transfer Authorisation) data feed.
     *
     * @param array<string, mixed> $queryParams
     * @return array<string, mixed>|array<never, never>
     */
    public function sta(array $queryParams): array
    {
        if (!$this->isAllowed()) {
            return [];
        }

        $searchModel  = new \PortalPeople\datadash\models\StaSearch();
        $dataProvider = $searchModel->search($queryParams);

        return $this->serializer()->serialize($dataProvider);
    }

    /**
     * SLA (Service Level Agreement) data feed.
     *
     * @param array<string, mixed> $queryParams
     * @return array<string, mixed>|array<never, never>
     */
    public function sla(array $queryParams): array
    {
        if (!$this->isAllowed()) {
            return [];
        }

        $searchModel  = new \PortalPeople\datadash\models\SlaSearch();
        $dataProvider = $searchModel->search($queryParams);

        return $this->serializer()->serialize($dataProvider);
    }
}
