<?php

namespace common\services;

use common\models\Company;
use common\models\ProductCategory;
use Yii;
use yii\helpers\ArrayHelper;

class CompanyService
{
    public Company $model;

    function __construct()
    {
        $this->model = new Company();
    }

    /**
     * @param int|null $id
     * @return array
     */
    public function getCompaniesByUser(int $id = null): array
    {
        return $this->model->find()->where(['user_id' => $id ?? Yii::$app->user->id])->all();
    }

    /**
     * @param int|null $id
     * @return array
     */
    public function getCompaniesByUserArr(int $id = null): array
    {
        return ArrayHelper::map($this->getCompaniesByUser($id), 'id', 'name');
    }

    /**
     * @param int $id
     * @return Company|null
     */
    public function getCompany(int $id): ?Company
    {
        return $this->model->findOne($id);
    }

    /**
     * @param int|null $id
     * @return array
     */
    public function getAddressesByUser(int $id = null): array
    {
        $companies = $this->getCompaniesByUser($id);
        return $this->model->find()->where(['id' => ArrayHelper::getColumn($companies, 'id')])->all();
    }

    /**
     * @param int|null $id
     * @return array
     */
    public function getAddressesByUserArr(int $id = null): array
    {
        return ArrayHelper::map($this->getAddressesByUser($id), 'id', 'address');
    }

    /**
     * @param int $id
     * @return array
     */
    public function getCategoryByCompanyId(int $id): array
    {
        return ProductCategory::find()->where(['company_id' => $id])->all();
    }


}