<?php

// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: DiffusionsController.php,v 1.14.6.2.2.3 2026/04/21 14:46:28 rtigero Exp $

namespace Pmb\DSI\Opac\Controller;

use Pmb\Common\Exceptions\NotFoundException;
use Pmb\Common\Exceptions\UnableToDeleteException;
use Pmb\Common\Helper\Response;
use Pmb\Common\Opac\Controller\Controller;
use Pmb\DSI\Models\Diffusion;
use Pmb\DSI\Models\SubscriberList\RootSubscriberList;
use Pmb\DSI\Models\SubscriberList\Subscribers\Subscriber;
use Pmb\DSI\Opac\Models\OpacDiffusions;
use Pmb\DSI\Opac\Views\AlertsView;
use Pmb\DSI\Opac\Views\UnsubscribeView;
use Pmb\DSI\Orm\DiffusionOrm;

class DiffusionsController extends Controller
{
    protected $action;

    protected $empr;

    protected $opacDiffusions;

    public function __construct(?object $data = null)
    {
        parent::__construct($data);
        if ($this->data->id) {
            $this->empr = new \emprunteur(intval($this->data->id));
        }
        $this->opacDiffusions = new OpacDiffusions($this->data->id ?? 0);
    }

    /**
     *
     * @param string $action
     * @return mixed
     */
    public function proceed(string $action = "")
    {
        try {
            return $this->callAction($action);
        } catch (NotFoundException $e) {
            trigger_error($e->getMessage(), E_USER_WARNING);
            Response::notFound();
        } catch (UnableToDeleteException $e) {
            trigger_error($e->getMessage(), E_USER_WARNING);
            Response::forbidden();
        }
    }

    protected function callAction(string $action)
    {
        switch ($action) {
            case "unsubscribe":
                $viewData = [
                    "diffusion" => null,
                ];
                if (! DiffusionOrm::exist($this->data->idDiffusion)) {
                    $view = new UnsubscribeView("dsi/unsubscribe", $viewData);
                    print $view->render();
                    exit();
                }
                $diffusion = new Diffusion($this->data->idDiffusion);
                $id = $this->data->idEmpr ?? $this->data->id;
                $viewData["diffusion"] = $diffusion;

                if ($id) {
                    $view = new UnsubscribeView("dsi/unsubscribe", $viewData);

                    //Cas d'une DSI privée, on supprime la diffusion à la désinscription
                    if ($diffusion->settings->idEmpr == $id) {
                        $diffusion->delete();
                        print $view->render();
                        exit();
                    }
                    $diffusion->fetchSubscriberList();
                    RootSubscriberList::unsubscribe($diffusion, $id, $this->data->emprType);
                    print $view->render();
                }
                break;
            case "bannette":
                global $opac_url_base;

                $opacDiffusions = $this->getOpacDiffusions();
                $opacDiffusionsPrivate = $this->getOpacDiffusionsPrivate();

                $subscriber = $this->getSubscriberFromType();

                $viewData = [
                    "list" => $opacDiffusions,
                    "listPrivate" => $opacDiffusionsPrivate,
                    "subscriber" => $subscriber,
                    "emprType" => $this->data->emprType,
                    "url_base" => $opac_url_base
                ];
                $view = new AlertsView("dsi/alerts", $viewData);
                print $view->render();
                break;
            default:
                return;
        }
    }

    public function getSubscriberFromType()
    {
        return $this->opacDiffusions->getSubscriberFromType($this->data->emprType, $this->data->id);
    }

    private function getOpacDiffusions()
    {
        return $this->opacDiffusions->getOpacDiffusions();
    }

    private function getOpacDiffusionsPrivate()
    {
        return $this->opacDiffusions->getOpacDiffusionsPrivate($this->data->emprType);
    }

    /**
     * Vérifie si un emprunteur est est base pour une entitée donnée
     *
     * @param $entityType int
     * @param $entityId int
     * @return Subscriber | null
     */
    public function getSubscriberFromEntity($entityType, $entityId)
    {
        return $this->opacDiffusions->getSubscriberFromEntity($entityType, $entityId);
    }
}
