<?php

declare(strict_types=1);

use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;

/**
 * @var string|null            $caption
 * @var Collection<int,Source> $sources
 * @var Tree                   $tree
 */

?>

<?php
// Count the number of linked records. These numbers include private records.
// It is not good to bypass privacy, but many servers do not have the resources
// to process privacy for every record in the tree
$count_individuals = DB::table('individuals')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'i_id');
        $join->on('l_file', '=', 'i_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy(['l_to'])
    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
    ->map(static fn (string $n): int => (int) $n)
    ->all();

$count_families = DB::table('families')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'f_id');
        $join->on('l_file', '=', 'f_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy(['l_to'])
    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
    ->map(static fn (string $n): int => (int) $n)
    ->all();

$count_media = DB::table('media')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'm_id');
        $join->on('l_file', '=', 'm_file');
    })
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy(['l_to'])
    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
    ->map(static fn (string $n): int => (int) $n)
    ->all();

$count_notes = DB::table('other')
    ->join('link', static function (JoinClause $join): void {
        $join->on('l_from', '=', 'o_id');
        $join->on('l_file', '=', 'o_file');
    })
    ->where('o_type', '=', 'NOTE')
    ->where('l_type', '=', 'SOUR')
    ->where('l_file', '=', $tree->id())
    ->groupBy(['l_to'])
    ->pluck(new Expression('COUNT(*) AS total'), 'l_to')
    ->map(static fn (string $n): int => (int) $n)
    ->all();
?>

<table
    id="wt-datatables-sources"
    class="table table-bordered table-sm wt-table-source datatables d-none"
    <?= view('lists/datatables-attributes') ?>
    data-columns="<?= e(json_encode([
        ['type' => 'html'],
        null,
        null,
        null,
        ['visible' => array_sum($count_individuals) > 0],
        ['visible' => array_sum($count_families) > 0],
        ['visible' => array_sum($count_media) > 0],
        ['visible' => array_sum($count_notes) > 0],
        ['visible' => (bool) $tree->getPreference('SHOW_LAST_CHANGE'), 'searchable' => false],
    ], JSON_THROW_ON_ERROR)) ?>"
>
    <caption class="visually-hidden">
        <?= $caption ?? I18N::translate('Sources') ?>
    </caption>

    <thead>
        <tr>
            <th><?= I18N::translate('Title') ?></th>
            <th class="d-none d-md-table-cell"><?= I18N::translate('Abbreviation') ?></th>
            <th class="d-none d-md-table-cell"><?= I18N::translate('Author') ?></th>
            <th class="d-none d-md-table-cell"><?= I18N::translate('Publication') ?></th>
            <th><?= I18N::translate('Individuals') ?></th>
            <th><?= I18N::translate('Families') ?></th>
            <th><?= I18N::translate('Media objects') ?></th>
            <th><?= I18N::translate('Shared notes') ?></th>
            <th><?= I18N::translate('Last change') ?></th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($sources as $source) : ?>
            <tr class="<?= $source->isPendingAddition() ? 'wt-new' : '' ?> <?= $source->isPendingDeletion() ? 'wt-old' : '' ?>">
                <!-- Title -->
                <td data-sort="<?= e($source->sortName()) ?>">
                    <a href="<?= e($source->url()) ?>">
                        <?= $source->fullName() ?>
                    </a>
                </td>

                <!-- Abbreviation -->
                <td class="d-none d-md-table-cell">
                    <?= e($source->facts(['ABBR'])->isNotEmpty() ? $source->facts(['ABBR'])->first()->value() : '') ?>
                </td>

                <!-- Author -->
                <td class="d-none d-md-table-cell">
                    <?= e($source->facts(['AUTH'])->isNotEmpty() ? $source->facts(['AUTH'])->first()->value() : '') ?>
                </td>

                <!-- Publisher -->
                <td class="d-none d-md-table-cell">
                    <?= Registry::elementFactory()->make('SOUR:PUBL')->value($source->facts(['PUBL'])->isNotEmpty() ? $source->facts(['PUBL'])->first()->value() : '', $tree) ?>
                </td>

                <!-- Count of linked individuals -->
                <td class="text-center" data-sort="<?= $count_individuals[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_individuals[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked families -->
                <td class="text-center" data-sort="<?= $count_families[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_families[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked media objects -->
                <td class="text-center" data-sort="<?= $count_media[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_media[$source->xref()] ?? 0) ?>
                </td>

                <!-- Count of linked notes -->
                <td class="text-center" data-sort="<?= $count_notes[$source->xref()] ?? 0 ?>">
                    <?= I18N::number($count_notes[$source->xref()] ?? 0) ?>
                </td>

                <!-- Last change -->
                <td data-sort="<?= sprintf('x%07d', $source->lastChangeTimestamp()->timestamp()) ?>">
                    <?= view('components/datetime', ['timestamp' => $source->lastChangeTimestamp()]) ?>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
