<?php
require_once __DIR__ . '/auth.php';

$pageTitle = 'Dashboard';

if (!function_exists('app_setting')) {
    function app_setting(PDO $pdo, string $key, string $default = ''): string {
        try {
            $stmt = $pdo->prepare("SELECT setting_value FROM app_settings WHERE setting_key = ? LIMIT 1");
            $stmt->execute([$key]);
            $value = $stmt->fetchColumn();
            return ($value !== false && $value !== null && $value !== '') ? (string)$value : $default;
        } catch (Throwable $e) {
            return $default;
        }
    }
}

$appName = app_setting($pdo, 'app_name', 'I Am Yielded Men');
$ministryName = app_setting($pdo, 'ministry_name', 'Yielded Vessels Ministries');
$appVersion = app_setting($pdo, 'app_version', '1.4');

$dashboardHeading = app_setting($pdo, 'dashboard_heading', 'Welcome');
$dashboardSubtitle = app_setting($pdo, 'dashboard_subtitle', 'This is your private ministry community platform.');
$footerText = app_setting($pdo, 'footer_text', '© iamyielded. All rights reserved. Platform - Private Use Only.');

$menuDirectory = app_setting($pdo, 'menu_directory', 'Directory');
$menuRotation = app_setting($pdo, 'menu_rotation', 'Prayer Rotation');
$menuPrayer = app_setting($pdo, 'menu_prayer', 'Prayer Guide');
$menuAssignments = app_setting($pdo, 'menu_assignments', 'Assignments');


if (!function_exists('dashboard_access_label')) {
    function dashboard_access_label(string $level): string {
        $labels = [
            'guest' => 'Guest',
            'member' => 'Member',
            'training' => 'Training',
            'leader' => 'Leader',
            'premium' => 'Premium',
            'admin' => 'Admin',
            'founder' => 'Founder'
        ];

        $level = strtolower(trim($level));
        return $labels[$level] ?? ucfirst($level ?: 'Member');
    }
}

$currentAccessLevel = 'member';

try {
    if (isset($pdo) && !empty($_SESSION['user_id'])) {
        $stmt = $pdo->prepare("SELECT access_level FROM users WHERE id = ? LIMIT 1");
        $stmt->execute([$_SESSION['user_id']]);
        $foundLevel = $stmt->fetchColumn();

        if ($foundLevel !== false && $foundLevel !== null && $foundLevel !== '') {
            $currentAccessLevel = (string)$foundLevel;
        }
    }
} catch (Throwable $e) {
    $currentAccessLevel = 'member';
}


require_once __DIR__ . '/includes/header.php';
?>

<main>
<section class="section">
<div class="container">

<div class="panel">

<div class="panel">
    <h2>Your Access Level</h2>
    <p class="muted">
        Current Access Level:
        <strong><?php echo h(dashboard_access_label($currentAccessLevel)); ?></strong>
    </p>
</div>

  <h1><?php echo h($dashboardHeading); ?></h1>
  <p class="muted"><?php echo h($dashboardSubtitle); ?></p>

  <div class="admin-grid">
    <a class="card" href="directory.php">
      <h3><?php echo h($menuDirectory); ?></h3>
      <p class="muted">Member contact list and connection area.</p>
    </a>

    <a class="card" href="rotation.php">
      <h3><?php echo h($menuRotation); ?></h3>
      <p class="muted">View current prayer rotation assignments.</p>
    </a>

    <a class="card" href="prayer.php">
      <h3><?php echo h($menuPrayer); ?></h3>
      <p class="muted">View prayer direction and focus.</p>
    </a>

    <a class="card" href="assignments.php">
      <h3><?php echo h($menuAssignments); ?></h3>
      <p class="muted">View current lessons, courses, and assignments.</p>
    </a>
  </div>
</div>

<div class="panel" style="margin-top:1rem;">
  <h2>Your Platform Setup</h2>
  <div class="admin-grid">
    <div class="card">
      <p class="muted">App Name</p>
      <h3><?php echo h($appName); ?></h3>
    </div>

    <div class="card">
      <p class="muted">Ministry Name</p>
      <h3><?php echo h($ministryName); ?></h3>
    </div>

    <div class="card">
      <p class="muted">Version</p>
      <h3><?php echo h($appVersion); ?></h3>
    </div>
  </div>
</div>

</div>
</section>
</main>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
