<?php
if (php_sapi_name() !== 'cli') {
    http_response_code(403);
    exit('Access denied.');
}

require_once __DIR__ . '/app/bootstrap.php';

use App\Core\Database;

try {
    $pdo = Database::getConnection();
    
    echo "=== 1. Subscription Plans ===\n";
    $plans = Database::select("SELECT * FROM subscription_plans");
    if (empty($plans)) {
        echo "No subscription plans found.\n";
    } else {
        foreach ($plans as $p) {
            echo "ID: {$p['id']} | Name: {$p['name']} | Price: {$p['price']} | Max Sessions: {$p['max_sessions']} | Max Requests: {$p['max_requests']} | Max Voice: {$p['max_voice_requests']} | Max OCR: {$p['max_ocr_requests']}\n";
        }
    }
    
    echo "\n=== 2. Companies ===\n";
    $companies = Database::select("SELECT * FROM companies LIMIT 10");
    if (empty($companies)) {
        echo "No companies found.\n";
    } else {
        foreach ($companies as $c) {
            echo "ID: {$c['id']} | Name: {$c['name']}\n";
        }
    }
    
    echo "\n=== 3. Active Company Subscriptions ===\n";
    $subs = Database::select("
        SELECT cs.*, p.name as plan_name 
        FROM company_subscriptions cs 
        JOIN subscription_plans p ON cs.plan_id = p.id
    ");
    if (empty($subs)) {
        echo "No subscriptions found.\n";
    } else {
        foreach ($subs as $s) {
            echo "ID: {$s['id']} | Company ID: {$s['company_id']} | Plan: {$s['plan_name']} (ID: {$s['plan_id']}) | Status: {$s['status']} | Starts: {$s['starts_at']} | Ends: {$s['ends_at']}\n";
        }
    }
    
    echo "\n=== 4. WooCommerce Stores ===\n";
    $stores = Database::select("SELECT id, company_id, store_url, is_active, webhook_secret FROM woocommerce_stores");
    if (empty($stores)) {
        echo "No WooCommerce stores found.\n";
    } else {
        foreach ($stores as $st) {
            echo "ID: {$st['id']} | Company ID: {$st['company_id']} | URL: {$st['store_url']} | Active: {$st['is_active']} | Webhook Secret: " . ($st['webhook_secret'] ? "YES" : "NO") . "\n";
        }
    }

} catch (\Exception $e) {
    echo "Error querying database: " . $e->getMessage() . "\n";
}
