<?php

namespace common\logic\User\UserProducer\Repository;

use common\logic\AbstractRepository;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer;

class UserProducerRepository extends AbstractRepository
{
    protected UserProducerRepositoryQuery $query;

    public function loadDependencies(): void
    {
        $this->query = $this->loadService(UserProducerRepositoryQuery::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [
            'with' => [],
            'join_with' => [],
            'orderby' => '',
            'attribute_id_producer' => 'user_producer.id_producer'
        ];
    }

    public function findOneUserProducer(User $user, Producer $producer)
    {
        return UserProducer::searchOne([
            'id_user' => $user->id,
            'user_producer.id_producer' => $producer->id
        ]);
    }

    public function findUserProducersByUser(User $user, bool $active = true, bool $bookmark = true)
    {
        return UserProducer::find()
            ->with(['producer'])
            ->where([
                'id_user' => $user->id,
                'active' => $active,
                'bookmark' => $bookmark
            ])
            ->all();
    }
}