<?php

namespace App\Repositories;

use App\Models\ProfitCenter;
use App\Models\Supplier;

class ProfitCenterRepository extends RepositoryBase implements ProfitCenterRepositoryInterface
{
    /**
     * @var array[]
     */
    private array $defaultRelations = ['contact', 'attachment'];

    public function __construct()
    {
        parent::__construct(ProfitCenter::class, $this->defaultRelations);
    }

    private ?ContactRepository $RepositoryContact = null;

    private function initContactRepository(): ?ContactRepository
    {
        if (! $this->RepositoryContact) {
            $this->RepositoryContact = new ContactRepository;
        }

        return $this->RepositoryContact;
    }

    private ?AddressRepository $RepositoryAddress = null;

    private function initAddressRepository(): ?AddressRepository
    {
        if (! $this->RepositoryAddress) {
            $this->RepositoryAddress = new AddressRepository;
        }

        return $this->RepositoryAddress;
    }

    public function addContact($supplierId, $contactIds = []): bool
    {
        $repo = $this->initContactRepository();
        foreach ((array) $contactIds as $contactId) {
            $repo->setContactRelation($contactId, $supplierId, $this->Model);
        }

        return true;
    }

    public function removeContact($supplierId, $contactIds = []): bool
    {
        $repo = $this->initContactRepository();
        foreach ((array) $contactIds as $contactId) {
            $repo->unsetContactRelation($contactId, $supplierId, $this->Model);
        }

        return true;
    }

    public function syncContact($supplierId, $contactIds = []): bool
    {
        // $model=new Supplier();
        // $model=new $this->Model();
        $item = $this->Model::find($supplierId);

        return $item->syncContactRelations((array) $contactIds);
    }

    public function syncRelationItem($id, $relId, $relType): bool
    {
        $item = $this->Model::find($id);

        return $item->syncRelations((array) $relId, $relType);
    }

    public function addRelationItem($id, $relIds, $relType): bool
    {
        $initFunctionName = 'init'.$relType.'Repository';
        $repo = $this->$initFunctionName();
        foreach ((array) $relIds as $relId) {
            $repo->setRelationItem($relId, $id, $this->Model);
        }

        return true;
    }

    public function unsetRelationItem($id, $relIds, $relType): bool
    {
        $initFunctionName = 'init'.$relType.'Repository';
        $repo = $this->$initFunctionName();
        foreach ((array) $relIds as $relId) {
            $repo->unsetRelationItem($relId, $id, $this->Model);
        }

        return true;
    }

    public function addProductGroup($supplierId, $productGroupIds = []): bool
    {
        $item = $this->Model::find($supplierId);
        $item->productGroup()->syncWithoutDetaching($productGroupIds);

        return true;
    }

    public function removeProductGroup($supplierId, $productGroupIds = []): bool
    {
        $item = $this->Model::find($supplierId);
        $item->productGroup()->detach($productGroupIds);

        return true;
    }

    public function syncProductGroup($supplierId, $productGroupIds = []): bool
    {
        $item = $this->Model::find($supplierId);
        $item->productGroup()->sync($productGroupIds);

        return true;
    }

    public function syncUser($id, $userId)
    {
        $item = $this->Model::find($id);

        return $item->users()->syncWithoutDetaching($userId);
    }

    public function syncSupplier($id, $attachedIds = []): bool
    {
        $item = $this->Model::find($id);
        $item->suppliers()->sync($attachedIds);

        return true;
    }

    /**
     * Get's a record by it's ID
     *
     * @param int
     * @return collection
     */
    /*    public function getWithRelations($id){

            if($data=$this->getQueryWithRelations()->where('id',$id)->get()->first()){
                if(count($data->attachment)>0){
                    $data['logoImgUrl']=$data->attachment[0]['publicUrl'];
                }

            }
            return $data;
        }*/
}
