<?php

namespace App\Repositories;

use App\Models\Supplier;

class SupplierRepository extends RepositoryBase implements SupplierRepositoryInterface
{
    /**
     * @var array[]
     */
    private array $defaultRelations = ['contact', 'productGroup', 'attachment', 'service'];

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

    private ?ContactRepository $RepositoryContact = null;

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

        return $this->RepositoryContact;
    }

    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 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 syncService($supplierId, $productGroupIds = []): bool
    {
        $item = $this->Model::find($supplierId);
        $item->service()->sync($productGroupIds);

        return true;
    }

    public function syncProfitCenter($supplierId, $profitCenterIds = []): bool
    {
        $item = $this->Model::find($supplierId);
        $item->profitCenter()->sync($profitCenterIds);

        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;
        }*/
}
