<?php

declare(strict_types=1);

use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ManageTrees;
use Fisharebest\Webtrees\Http\RequestHandlers\MergeFactsPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;

/**
 * @var array<string,array<int,array<int,GedcomRecord>>> $duplicates
 * @var Tree                                             $tree
 * @var string                                           $title
 */

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route(ManageTrees::class, ['tree' => $tree->name()]) => I18N::translate('Manage family trees'), $title]]) ?>

<h1><?= $title ?></h1>

<?php foreach ($duplicates as $category => $records) : ?>
    <h2><?= $category ?></h2>

    <?php if (!empty($records)) : ?>
        <ol>
            <?php foreach ($records as $duplicates) : ?>
                <li>
                    <?= $duplicates[0]->fullName() ?>
                    <?php foreach ($duplicates as $record) : ?>
                        —
                        <a href="<?= e($record->url()) ?>">
                            <?= $record->xref() ?>
                        </a>
                    <?php endforeach ?>
                    <?php if (count($duplicates) === 2) : ?>
                        —
                        <a href="<?= e(route(MergeFactsPage::class, ['tree' => $tree->name(), 'xref1' => $duplicates[0]->xref(), 'xref2' => $duplicates[1]->xref()])) ?>">
                            <?= I18N::translate('Merge') ?>
                        </a>
                    <?php endif ?>
                </li>
            <?php endforeach ?>
        </ol>
    <?php else : ?>
        <p><?= I18N::translate('No duplicates have been found.') ?></p>
    <?php endif ?>
<?php endforeach ?>
