<?php

require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/app/platform-admin.php';

$pdo = app_db_pdo();
$user = app_require_platform_admin($pdo);

$tenantId = filter_input(INPUT_GET, 'tenant_id', FILTER_VALIDATE_INT);
if ($tenantId === null || $tenantId === false || $tenantId <= 0) {
    http_response_code(400);
    exit('Ungültige Mandanten-ID.');
}

$detail = app_backoffice_fetch_tenant_detail($pdo, $tenantId);
if ($detail === null) {
    http_response_code(404);
    exit('Mandant wurde nicht gefunden.');
}

// Jede Ansicht eines Mandanten durch das Back-Office wird im Audit-Log
// dieses Mandanten protokolliert, damit der Zugriff fuer den Kunden
// nachvollziehbar bleibt (siehe AVV-Transparenzpflicht).
app_audit_log($pdo, $tenantId, (int)$user['user_id'], 'backoffice.tenant_viewed', 'tenant', $tenantId);

$rollenLabels = ['owner' => 'Inhaber', 'admin' => 'Administrator', 'treasurer' => 'Kassenwart', 'member' => 'Mitglied', 'viewer' => 'Betrachter'];

include 'header.php';
include 'headerline.php';
include 'nav.php';
?>

<section id="banner">
    <div class="content">
        <h2>Back-Office: <?php echo saas_html($detail['tenant']['name']); ?></h2>
        <p><a href="backoffice.php">&larr; Zurück zur Mandantenübersicht</a></p>

        <table>
            <tr><th>Kürzel</th><td><?php echo saas_html($detail['tenant']['slug']); ?></td></tr>
            <tr><th>Status</th><td><?php echo saas_html($detail['tenant']['status']); ?></td></tr>
            <tr><th>Erstellt</th><td><?php echo saas_html($detail['tenant']['created_at']); ?></td></tr>
            <?php if ($detail['settings'] !== null): ?>
                <tr><th>Preis pro Strich</th><td><?php echo saas_html(saas_format_money_cents((int)$detail['settings']['mark_price_cents'])); ?> €</td></tr>
                <tr><th>PayPal aktiv</th><td><?php echo (int)$detail['settings']['paypal_enabled'] === 1 ? 'ja' : 'nein'; ?></td></tr>
            <?php endif; ?>
        </table>

        <form method="post" action="backoffice-export.php">
            <?php echo app_csrf_field(); ?>
            <input type="hidden" name="tenant_id" value="<?php echo (int)$tenantId; ?>">
            <button type="submit">Datenexport dieses Mandanten herunterladen</button>
        </form>

        <h3>Mitglieder mit Zugang</h3>
        <table>
            <tr><th>Name</th><th>E-Mail</th><th>Rolle</th><th>Status</th></tr>
            <?php if ($detail['members'] === []): ?>
                <tr><td colspan="4">Kein Mitglied mit Login-Zugang.</td></tr>
            <?php endif; ?>
            <?php foreach ($detail['members'] as $member): ?>
                <tr>
                    <td><?php echo saas_html($member['display_name']); ?></td>
                    <td><?php echo saas_html($member['email']); ?></td>
                    <td><?php echo saas_html($rollenLabels[$member['role']] ?? $member['role']); ?></td>
                    <td><?php echo saas_html($member['membership_status']); ?></td>
                </tr>
            <?php endforeach; ?>
        </table>

        <h3>Letzte Buchungen</h3>
        <table>
            <tr><th>Datum</th><th>Mitglied</th><th>Typ</th><th>Betrag</th><th>Quelle</th></tr>
            <?php if ($detail['recent_entries'] === []): ?>
                <tr><td colspan="5">Keine Buchungen vorhanden.</td></tr>
            <?php endif; ?>
            <?php foreach ($detail['recent_entries'] as $entry): ?>
                <tr>
                    <td><?php echo saas_html($entry['booked_at']); ?></td>
                    <td><?php echo saas_html($entry['display_name']); ?></td>
                    <td><?php echo saas_html($entry['type']); ?></td>
                    <td><?php echo saas_html(saas_format_money_cents((int)$entry['amount_cents'])); ?> €</td>
                    <td><?php echo saas_html($entry['source']); ?></td>
                </tr>
            <?php endforeach; ?>
        </table>

        <h3>Letzte Admin-Aktionen</h3>
        <table>
            <tr><th>Datum</th><th>Wer</th><th>Aktion</th></tr>
            <?php if ($detail['recent_audit'] === []): ?>
                <tr><td colspan="3">Noch keine protokollierten Aktionen.</td></tr>
            <?php endif; ?>
            <?php foreach ($detail['recent_audit'] as $entry): ?>
                <tr>
                    <td><?php echo saas_html($entry['created_at']); ?></td>
                    <td><?php echo saas_html($entry['actor_name'] ?? '—'); ?></td>
                    <td><?php echo saas_html($entry['action']); ?></td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>
</section>

<?php include 'footer.php'; ?>
