<?php

namespace Modules\ProjetAchat\Service;

use Modules\ProjetAchat\Repository\Company;
use Modules\Ajax\Repository\Listes;

class CompanyService
{
  
    private $pdo;

    /**
     * @var Company $companyRepository
     */
    private $companyRepository;

    public function __construct($pdo)
    {
        $this->pdo = $pdo;
        $this->companyRepository = new Company($this->pdo);
    }

    /**
     * @return array<mixed>
    */
    public function findCompanies(bool $multiCompanies, string $typeCompany): ?array
    {
        if ('G' === $typeCompany) {
            $listeRepoAjax = new Listes($this->pdo);
            return $listeRepoAjax->findGroupedCompanies();
        }
        return $this->companyRepository->findCompanies($multiCompanies);
    }

    /**
     * @return array<mixed>
    */
    public function findCompaniesAttachedToModel(Int $idModel, string $typeCompany): ?array
    {
        return $this->companyRepository->findCompaniesAttachedToModel($idModel, $typeCompany);
    }

    /**
     * @return array<mixed>
    */
    public function findExplicitCompaniesLinkedToModel(Int $idModel, string $typeCompany): ?array
    {
        if ($typeCompany === 'G') {
            return $this->companyRepository->findExplicitCompaniesGroupsLinkedToModel($idModel, $typeCompany);
        }
        return $this->companyRepository->findExplicitCompaniesLinkedToModel($idModel, $typeCompany);
    }

    public function linkModel(Int $idModel, string $idCompany, string $typeCompany) : void
    {
        $this->companyRepository->linkModel($idModel, $idCompany, $typeCompany);
    }

    public function unlinkModel(Int $idModel, string $idCompany, string $typeCompany) : void
    {
        $this->companyRepository->unlinkModel($idModel, $idCompany, $typeCompany);
    }

    public function unlinkAllModel(Int $idModel, string $typeCompany) : void
    {
        $this->companyRepository->unlinkAllModel($idModel, $typeCompany);
    }
}
