<?php
namespace Incoviba\Service\Inmobiliaria;

use Incoviba\Exception\ServiceAction\Read;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;

class Proveedor extends Ideal\Service
{
    public function __construct(LoggerInterface $logger,
                                protected Repository\Inmobiliaria\Proveedor $proveedorRepository,
                                protected Repository\Inmobiliaria\Proveedor\Datos $datosRepository,
                                protected Service\Persona $contactoService)
    {
        parent::__construct($logger);
    }

    public function getAll(?string $orderBy = null): array
    {
        try {
            return array_map([$this, 'process'], $this->proveedorRepository->fetchAll($orderBy));
        } catch (Implement\Exception\EmptyResult) {
            return [];
        }
    }

    public function getById(int $proveedor_rut): ?Model\Inmobiliaria\Proveedor
    {
        try {
            return $this->process($this->proveedorRepository->fetchById($proveedor_rut));
        } catch (Implement\Exception\EmptyResult) {
            return null;
        }
    }

    public function add(array $data): Model\Inmobiliaria\Proveedor
    {
        $filteredData = $this->proveedorRepository->filterData($data);
        try {
            $proveedor = $this->process($this->proveedorRepository->fetchByNombre($filteredData['nombre']));
            if (!empty($data['contacto']['rut']) and $proveedor->contacto->rut !== $data['contacto']['rut']) {
                $contacto = $this->contactoService->add($data['contacto']);
                return $this->proveedorRepository->edit($proveedor, ['contacto_rut' => $contacto->rut]);
            }
            return $proveedor;
        } catch (Implement\Exception\EmptyResult) {
            if (!empty($data['contacto']['rut'])) {
                $contacto = $this->contactoService->add($data['contacto']);
                $filteredData['contacto_rut'] = $contacto->rut;
            }
            $proveedor = $this->proveedorRepository->create($filteredData);
            return $this->process($this->proveedorRepository->save($proveedor));
        }
    }
    /**
     * @param Model\Inmobiliaria\Proveedor $proveedor
     * @param array $data
     * @return Model\Inmobiliaria\Proveedor
     */
    public function edit(Model\Inmobiliaria\Proveedor $proveedor, array $data): Model\Inmobiliaria\Proveedor
    {
        try {
            if (!empty($data['contacto']['rut'])) {
                $contacto = $this->contactoService->getById($data['contacto']['rut']);
                $contacto = $this->contactoService->edit($contacto, $data['contacto']);
                $data['contacto_rut'] = $contacto->rut;
            } elseif ($proveedor->contacto !== null) {
                $data['contacto_rut'] = $proveedor->contacto->rut;
            }
            $filteredData = $this->proveedorRepository->filterData($data);
            return $this->process($this->proveedorRepository->edit($proveedor, $filteredData));
        } catch (Read | Implement\Exception\EmptyResult) {
            return $proveedor;
        }
    }
    public function delete(Model\Inmobiliaria\Proveedor $proveedor): bool
    {
        try {
            $this->proveedorRepository->remove($proveedor);
            return true;
        } catch (PDOException) {
            return false;
        }
    }

    protected function process(Model\Inmobiliaria\Proveedor $proveedor): Model\Inmobiliaria\Proveedor
    {
        $proveedor->addFactory('datos', (new Implement\Repository\Factory())->setCallable([$this->datosRepository, 'fetchByProveedor'])->setArgs(['proveedor_rut' => $proveedor->rut]));
        return $proveedor;
    }
}
