<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Controller;

use Doctrine\Persistence\ManagerRegistry;
use Mautic\CoreBundle\Controller\CommonController;
use MauticPlugin\DealFlowBundle\Controller\Trait\OwnerChoicesTrait;
use MauticPlugin\DealFlowBundle\Entity\Deal;
use MauticPlugin\DealFlowBundle\Entity\DealRepository;
use MauticPlugin\DealFlowBundle\Service\DealAuthorization;
use MauticPlugin\DealFlowBundle\Service\DealContextBuilder;
use MauticPlugin\DealFlowBundle\Service\DealFieldManager;
use MauticPlugin\DealFlowBundle\Service\DealTimelineBuilder;
use MauticPlugin\DealFlowBundle\Service\DefaultPipelineInitializer;
use Symfony\Component\HttpFoundation\Response;

/**
 * Handles the single deal detail view.
 * Consumed by DealController (facade) — do not register routes here directly.
 */
final class DealViewController extends CommonController
{
    use OwnerChoicesTrait;

    public function viewAction(
        int $objectId,
        DealAuthorization $authorization,
        ManagerRegistry $managerRegistry,
        DealRepository $dealRepository,
        DealTimelineBuilder $timelineBuilder,
        DealContextBuilder $contextBuilder,
        DealFieldManager $dealFieldManager,
        DefaultPipelineInitializer $pipelineInitializer,
    ): Response {
        if (!$authorization->canViewDeals()) {
            return $this->accessDenied();
        }

        $deal = $dealRepository->find($objectId);
        if (!$deal instanceof Deal) {
            throw $this->createNotFoundException(sprintf('Deal %d not found.', $objectId));
        }

        if (!$authorization->canViewDeal($deal)) {
            return $this->accessDenied();
        }

        $pipeline = $deal->getPipeline();
        $stages = $pipeline ? $pipelineInitializer->getOrderedStages($pipeline, true) : [];

        return $this->delegateView([
            'viewParameters' => [
                'deal' => $deal,
                'dealContext' => $contextBuilder->buildForDeal($deal),
                'timeline' => $timelineBuilder->build($deal),
                'backUrl' => $this->generateUrl('mautic_dealflow_pipeline', ['pipelineId' => $deal->getPipeline()?->getId()]),
                'taskOwnerChoices' => $this->buildOwnerChoices($managerRegistry),
                'taskOwnerLabels' => array_flip($this->buildOwnerChoices($managerRegistry)),
                'noteAuthorLabels' => array_flip($this->buildOwnerChoices($managerRegistry)),
                'canEditDeal' => $authorization->canEditDeal($deal),
                'customFieldDisplayRows' => $dealFieldManager->buildDisplayRows($deal),
                'stages' => $stages,
            ],
            'contentTemplate' => '@DealFlow/Deal/view.html.twig',
            'passthroughVars' => [
                'activeLink' => '#mautic_dealflow_pipeline',
                'route' => $this->generateUrl('mautic_dealflow_view', ['objectId' => $deal->getId()]),
                'mauticContent' => 'dealflow',
            ],
        ]);
    }
}
