<?php

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';

/**
 * Assembles a full export of everything stored for one tenant (data
 * portability / GDPR Art. 20). Excludes password_hash and token hashes;
 * everything else that is tenant-scoped is included as-is.
 *
 * @return array<string, mixed>
 */
function app_export_tenant_data(PDO $pdo, int $tenantId): array
{
    $stmt = $pdo->prepare('SELECT id, slug, name, status, timezone, locale, currency_code, created_at, updated_at FROM tenants WHERE id = ?');
    $stmt->execute([$tenantId]);
    $tenant = $stmt->fetch();

    $stmt = $pdo->prepare('SELECT * FROM tenant_settings WHERE tenant_id = ?');
    $stmt->execute([$tenantId]);
    $settings = $stmt->fetch();

    $stmt = $pdo->prepare('SELECT * FROM participants WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $participants = $stmt->fetchAll();

    $stmt = $pdo->prepare(
        'SELECT u.id, u.email, u.display_name, u.status, u.email_verified_at, u.last_login_at, u.created_at,
                tm.role, tm.status AS membership_status, tm.invited_at, tm.joined_at
         FROM tenant_memberships tm
         JOIN users u ON u.id = tm.user_id
         WHERE tm.tenant_id = ?
         ORDER BY u.id'
    );
    $stmt->execute([$tenantId]);
    $members = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT * FROM ledger_entries WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $ledgerEntries = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT id, message, valid_from, valid_until, deleted_at, created_at FROM notices WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $notices = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT id, original_filename, status, created_at, committed_at FROM payment_import_batches WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $importBatches = $stmt->fetchAll();

    $stmt = $pdo->prepare(
        'SELECT r.id, r.batch_id, r.row_num, r.raw_name, r.amount_cents, r.booked_at, r.status
         FROM payment_import_rows r
         JOIN payment_import_batches b ON b.id = r.batch_id
         WHERE b.tenant_id = ?
         ORDER BY r.id'
    );
    $stmt->execute([$tenantId]);
    $importRows = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT id, participant_id, template, subject, status, sent_at, error, created_at FROM outbound_emails WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $outboundEmails = $stmt->fetchAll();

    $stmt = $pdo->prepare('SELECT id, actor_user_id, action, subject_type, subject_id, metadata_json, ip, created_at FROM audit_log WHERE tenant_id = ? ORDER BY id');
    $stmt->execute([$tenantId]);
    $auditLog = $stmt->fetchAll();

    return [
        'exported_at' => date('c'),
        'tenant' => $tenant ?: null,
        'tenant_settings' => $settings ?: null,
        'participants' => $participants,
        'members' => $members,
        'ledger_entries' => $ledgerEntries,
        'notices' => $notices,
        'payment_import_batches' => $importBatches,
        'payment_import_rows' => $importRows,
        'outbound_emails' => $outboundEmails,
        'audit_log' => $auditLog,
    ];
}
