<?php
declare(strict_types=1);
require __DIR__ . '/app/bootstrap.php';
require_auth();
require_role(['owner']);

$error = null;
$bootError = (string)($_SESSION['finance_boot_error'] ?? '');
unset($_SESSION['finance_boot_error']);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    try {
        require_once __DIR__ . '/app/migrations.php';
        if (billing_finance_schema_gaps() || isset($_POST['force_finance_repair'])) {
            $replay = [
                '20260713_zimbabwe_payments','20260713_suppliers_and_bills','20260713_sodazw_operations',
                '20260718_finance_control_centre','20260718_document_workflow_safety',
                '20260721_stability_security_v1','20260722_finance_centre_repair',
            ];
            $delete = db()->prepare('DELETE FROM schema_migrations WHERE version=?');
            foreach ($replay as $version) $delete->execute([$version]);
        }
        run_migrations(db());
        $remaining = billing_finance_schema_gaps();
        if ($remaining) throw new RuntimeException('Repair is incomplete: '.implode(', ',array_slice($remaining,0,10)));
        audit('database_upgraded', 'system', null, BILLING_BUILD_ID);
        flash('success', 'Finance Centre setup completed for build ' . BILLING_BUILD_ID . '.');
        redirect('index.php?page=finance');
    } catch (Throwable $e) {
        $error = 'The upgrade stopped safely: ' . $e->getMessage();
    }
}
$current = billing_schema_is_current();
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex,nofollow"><title>Finance Centre setup - Soda ZW Billing</title><link rel="stylesheet" href="assets/accounting-os-v6.css?v=<?=e(BILLING_BUILD_ID)?>"><link rel="stylesheet" href="assets/sodazw-premium-v7.css?v=<?=e(BILLING_BUILD_ID)?>"><link rel="stylesheet" href="assets/sodazw-v12.css?v=<?=e(BILLING_BUILD_ID)?>"></head>
<body class="auth-page accounting-v6 premium-v7 sodazw-v12"><main class="auth-card"><img src="assets/sodazw-logo.png" alt="Soda ZW"><span class="eyebrow">Owner-controlled maintenance</span><h1>Complete the Finance Centre setup</h1><p>This controlled repair installs the accounting-control tables required for credits, reconciliation, reminders, receiving and backups.</p><p>Build <strong><?=e(BILLING_BUILD_ID)?></strong></p><?php if($bootError):?><div class="alert error" role="alert"><?=e($bootError)?></div><?php endif;?><?php if($error):?><div class="alert error" role="alert"><?=e($error)?></div><?php endif;?><?php if($current&&!isset($_GET['force'])):?><div class="alert success">Finance Centre is installed and current.</div><a class="button primary" href="index.php?page=finance">Open Finance Centre</a><a class="button secondary" href="upgrade.php?force=1">Run diagnostic repair</a><?php else:?><div class="alert info">Back up the database first. The repair is safe to run more than once and records its schema version when complete.</div><form method="post"><?=csrf_field()?><input type="hidden" name="force_finance_repair" value="1"><button class="button primary" type="submit">Install and repair Finance Centre</button></form><?php endif;?></main></body></html>
