<?php

declare(strict_types=1);

require_once __DIR__ . '/includes/platform-bootstrap.php';

platform_require_login($pdo);

$page = max(
    1,
    (int) ($_GET['page'] ?? 1)
);

$perPage = 100;
$offset = ($page - 1) * $perPage;

$statement = $pdo->prepare(
    'SELECT
        logs.id,
        logs.action,
        logs.description,
        logs.metadata_json,
        logs.ip_address,
        logs.created_at,
        admins.username,
        admins.display_name,
        organizations.name AS organization_name,
        logs.support_session_id
     FROM platform_audit_logs AS logs
     LEFT JOIN platform_admins AS admins
        ON admins.id =
            logs.platform_admin_id
     LEFT JOIN organizations
        ON organizations.id =
            logs.organization_id
     ORDER BY logs.created_at DESC,
              logs.id DESC
     LIMIT :limit_value
     OFFSET :offset_value'
);

$statement->bindValue(
    ':limit_value',
    $perPage,
    PDO::PARAM_INT
);

$statement->bindValue(
    ':offset_value',
    $offset,
    PDO::PARAM_INT
);

$statement->execute();
$logs = $statement->fetchAll();

$total = (int) $pdo
    ->query(
        'SELECT COUNT(*)
         FROM platform_audit_logs'
    )
    ->fetchColumn();

$platformPageTitle = 'Kehittäjäloki';
$platformActivePage = 'audit-log';

require __DIR__ . '/includes/header.php';

?>
<div class="platform-page-heading">
    <div>
        <p class="platform-eyebrow">
            Muuttumaton tapahtumahistoria
        </p>

        <h1>Kehittäjäloki</h1>

        <p class="platform-description">
            Kirjautumiset, yritysten luonti, tilamuutokset ja kaikki tukikäynnit tallennetaan tänne.
        </p>
    </div>
</div>

<section class="platform-panel">
    <?php if ($logs === []): ?>
        <div class="platform-empty">
            Ei lokitapahtumia.
        </div>
    <?php else: ?>
        <div class="platform-table-wrap">
            <table class="platform-table">
                <thead>
                    <tr>
                        <th>Aika</th>
                        <th>Tapahtuma</th>
                        <th>Kehittäjä</th>
                        <th>Yritys</th>
                        <th>IP</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($logs as $log): ?>
                        <tr>
                            <td>
                                <?= platform_e(
                                    date(
                                        'd.m.Y H:i:s',
                                        strtotime(
                                            (string) $log['created_at']
                                        )
                                    )
                                ) ?>
                            </td>

                            <td>
                                <strong>
                                    <?= platform_e(
                                        $log['action']
                                    ) ?>
                                </strong>

                                <span class="platform-table-secondary">
                                    <?= platform_e(
                                        $log['description']
                                        ?? ''
                                    ) ?>
                                </span>

                                <?php if (
                                    !empty(
                                        $log['support_session_id']
                                    )
                                ): ?>
                                    <span class="platform-table-secondary">
                                        Tukitila #
                                        <?= (int) $log['support_session_id'] ?>
                                    </span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <?= platform_e(
                                    $log['display_name']
                                    ?: $log['username']
                                    ?: 'Järjestelmä'
                                ) ?>
                            </td>

                            <td>
                                <?= platform_e(
                                    $log['organization_name']
                                    ?: '–'
                                ) ?>
                            </td>

                            <td>
                                <?= platform_e(
                                    $log['ip_address']
                                    ?: '–'
                                ) ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div
            class="platform-actions"
            style="margin-top: 18px;"
        >
            <?php if ($page > 1): ?>
                <a
                    class="platform-button platform-button-secondary"
                    href="/platform/audit-log.php?page=<?= $page - 1 ?>"
                >
                    Edellinen
                </a>
            <?php endif; ?>

            <?php if (
                $offset + count($logs)
                < $total
            ): ?>
                <a
                    class="platform-button platform-button-secondary"
                    href="/platform/audit-log.php?page=<?= $page + 1 ?>"
                >
                    Seuraava
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</section>

<?php

require __DIR__ . '/includes/footer.php';
