<?php
namespace Incoviba\Repository\Inmobiliaria;

use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;

class SociedadAgente extends Ideal\Repository
{
    public function __construct(Define\Connection $connection, protected Repository\Inmobiliaria $inmobiliariaRepository,
                                protected AgenteTipo $agenteRepository)
    {
        parent::__construct($connection);
        $this->setTable('sociedad_agente');
    }
    public function create(?array $data = null): Model\Inmobiliaria\SociedadAgente
    {
        $map = (new Implement\Repository\MapperParser())
            ->register('sociedad_rut', (new Implement\Repository\Mapper())
                ->setProperty('sociedad')
                ->setFunction(function(?array $data) {
                    return ($data === null) ? null : $this->inmobiliariaRepository->fetchById($data['sociedad_rut']);
                }))
            ->register('agente_tipo_id', (new Implement\Repository\Mapper())
                ->setProperty('agenteTipo')
                ->setFunction(function(?array $data) {
                    return ($data === null) ? null : $this->agenteRepository->fetchById($data['agente_tipo_id']);
                }));
        return $this->parseData(new Model\Inmobiliaria\SociedadAgente(), $data, $map);
    }
    public function save(Define\Model $model): Model\Inmobiliaria\SociedadAgente
    {
        $model->id = $this->saveNew([
            'sociedad_rut', 'agente_tipo_id'
        ], [
            $model->sociedad->rut, $model->agenteTipo->id
        ]);
        return $model;
    }
    public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\SociedadAgente
    {
        return $this->update($model, ['sociedad_rut', 'agente_tipo_id'], $new_data);
    }
    public function load(array $data_row): Define\Model
    {
        $model = $this->create($data_row);
        return $model;
    }

    public function fetchBySociedad(int $sociedad_rut): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select()
            ->from($this->getTable())
            ->where('sociedad_rut = ?');
        return $this->fetchMany($query, [$sociedad_rut]);
    }
    public function fetchByAgente(int $agente_id): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select('a.*')
            ->from("{$this->getTable()} a")
            ->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
            ->where('at.agente = ?');
        return $this->fetchMany($query, [$agente_id]);
    }
    public function fetchByTipo(int $tipo_agente_id): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select('a.*')
            ->from("{$this->getTable()} a")
            ->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
            ->where('at.tipo = ?');
        return $this->fetchMany($query, [$tipo_agente_id]);
    }
    public function fetchBySociedadAndTipo(int $sociedad_rut, int $tipo_agente_id): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select('a.*')
            ->from("{$this->getTable()} a")
            ->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
            ->where('sociedad_rut = ? AND at.tipo = ?');
        return $this->fetchMany($query, [$sociedad_rut, $tipo_agente_id]);
    }
    public function fetchBySociedadAndAgente(int $sociedad_rut, int $agente_id): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select('a.*')
            ->from("{$this->getTable()} a")
            ->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
            ->where('sociedad_rut = ? AND at.agente = ?');
        return $this->fetchMany($query, [$sociedad_rut, $agente_id]);
    }
    public function fetchBySociedadAndAgenteAndTipo(int $sociedad_rut, int $agente_id, int $tipo_agente_id): array
    {
        $query = $this->connection->getQueryBuilder()
            ->select('a.*')
            ->from("{$this->getTable()} a")
            ->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
            ->where('sociedad_rut = ? AND at.agente = ? AND at.tipo = ?');
        return $this->fetchMany($query, [$sociedad_rut, $agente_id, $tipo_agente_id]);
    }
}
