<?php
/**
 * Virtual Print Page Plugin
 *
 * Creates a virtual /print page for each narrative
 * Allows access to print editor via /projet/narrative/print
 */

use Kirby\Cms\Page;
use Kirby\Uuid\Uuid;

Kirby::plugin('geoproject/virtual-print-page', [
    'routes' => [
        [
            'pattern' => '(:all)/print',
            'action' => function ($parentPath) {
                // Find parent page (the narrative)
                $parent = page($parentPath);

                if (!$parent || $parent->intendedTemplate()->name() !== 'narrative') {
                    return $this->next();
                }

                // Create virtual page with Page::factory()
                return Page::factory([
                    'slug' => 'print',
                    'template' => 'print',
                    'parent' => $parent,
                    'content' => [
                        'title' => 'Print - ' . $parent->title()->value(),
                        'uuid' => Uuid::generate()
                    ]
                ]);
            }
        ]
    ]
]);
