<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
// ============================================================
//  debug_journals.php — Browser mein kholo check karne ke liye
//  URL: http://localhost/cijss_backend/ajax/debug_journals.php
//  Testing ke baad DELETE kar dena!
// ============================================================
require_once __DIR__ . '/../config/db.php';

echo "<pre style='font-family:monospace;font-size:13px;padding:20px'>";
echo "=== CIJSS DB DEBUG ===\n\n";

try {
    // 1. Connection check
    $ver = db()->query("SELECT VERSION()")->fetchColumn();
    echo "✅ DB Connected | MySQL Version: $ver\n\n";

    // 2. Journal count
    $total = db()->query("SELECT COUNT(*) FROM journals")->fetchColumn();
    echo "📊 Total journals in DB: $total\n";

    $active = db()->query("SELECT COUNT(*) FROM journals WHERE is_active = 1")->fetchColumn();
    echo "✅ is_active=1  : $active\n";

    $inactive = db()->query("SELECT COUNT(*) FROM journals WHERE is_active != 1")->fetchColumn();
    echo "❌ is_active!=1 : $inactive  ← Ye problem ho sakti hai!\n\n";

    // 3. Show all journals
    echo "--- All Journals ---\n";
    $rows = db()->query("SELECT journal_id, category_id, name, is_active FROM journals LIMIT 20")->fetchAll();
    foreach ($rows as $r) {
        $flag = $r['is_active'] == 1 ? '✅' : '❌';
        echo "$flag ID:{$r['journal_id']} | cat:{$r['category_id']} | is_active:{$r['is_active']} | {$r['name']}\n";
    }

    echo "\n--- Journal Categories ---\n";
    $cats = db()->query("SELECT * FROM journal_categories ORDER BY display_order")->fetchAll();
    foreach ($cats as $c) {
        echo "ID:{$c['category_id']} | {$c['name']}\n";
    }

    echo "\n--- FIX: Run this SQL to activate all journals ---\n";
    echo "UPDATE journals SET is_active = 1;\n";

} catch (PDOException $e) {
    echo "❌ DB Error: " . $e->getMessage() . "\n";
}
echo "</pre>";