<?php
if(!defined('BASEPATH')) {
   die('Direct access to the script is not allowed');
}

// 1. Admin Secure Inserter Hook
if (isset($_POST['action']) && $_POST['action'] == 'add_top_service_action') {
    $service_id = isset($_POST['service_id']) ? intval($_POST['service_id']) : 0;
    
    if ($service_id > 0) {
        $check = $conn->prepare("SELECT id FROM top_services WHERE service_id = ?");
        $check->execute([$service_id]);
        
        if ($check->rowCount() == 0) {
            $insert = $conn->prepare("INSERT INTO top_services (service_id) VALUES (?)");
            if($insert->execute([$service_id])) {
                echo json_encode(["status" => "success"]);
            } else {
                echo json_encode(["status" => "failed"]);
            }
        } else {
            echo json_encode(["status" => "exists"]);
        }
    } else {
        echo json_encode(["status" => "error"]);
    }
    exit;
}

// 2. Upgraded Frontend Layout Engine (Popup Free Architecture)
if (isset($_GET['fetch']) && $_GET['fetch'] == 'live_logs') {
    // Top Services fetch with automatic dynamic join mapping
    $query = $conn->query("
        SELECT s.service_id, s.service_name, s.service_min, s.service_max, c.category_name, t.created_at 
        FROM top_services t
        INNER JOIN services s ON t.service_id = s.service_id
        INNER JOIN categories c ON s.category_id = c.category_id
        ORDER BY t.id DESC LIMIT 30
    ");
    $top_list = $query->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($top_list)) {
        echo '<div style="padding: 40px; text-align: center; color: #a0aec0; font-weight: 600;"><i class="fa fa-info-circle mr-2 text-warning"></i> No recent service updates deployed.</div>';
    } else {
        echo '<div style="padding: 10px; display: flex; flex-direction: column; gap: 12px;">';
        foreach ($top_list as $row) {
            // Processing dynamic human readable date timestamps
            $formatted_date = date("d M, h:i A", strtotime($row['created_at']));
            
            echo '<div class="alix-service-card" style="background: #1e2538; border: 1px solid rgba(0, 240, 255, 0.15); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: 0 4px 10px rgba(0,0,0,0.15); transition: 0.2s;">';
                
                // Top Meta Header Row
                echo '<div style="display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 6px;">';
                    echo '<div style="display: flex; align-items: center; gap: 8px;">';
                        echo '<span onclick="copyServiceId('.$row['service_id'].')" title="Click to Copy ID" style="background: #007bff; color: #fff; font-weight: 800; font-size: 11px; padding: 3px 8px; border-radius: 4px; cursor: pointer; display: inline-flex; align-items: center; gap: 4px;">';
                            echo '#' . $row['service_id'] . ' <i class="fa fa-copy" style="font-size: 9px; opacity:0.8;"></i>';
                        echo '</span>';
                        echo '<span style="background: rgba(40, 167, 69, 0.15); color: #28a745; font-size: 10px; font-weight: 800; padding: 2px 6px; border-radius: 4px; text-transform: uppercase; border: 1px solid rgba(40,167,69,0.3);">WORKING</span>';
                    echo '</div>';
                    echo '<span style="font-size: 11px; color: #a0aec0; font-weight: 600;"><i class="far fa-clock text-warning mr-1"></i> ' . $formatted_date . '</span>';
                echo '</div>';
                
                // Content Context Body
                echo '<div style="font-size: 12.5px; line-height: 1.4;">';
                    echo '<div style="color: #ffcc00; font-weight: 700; margin-bottom: 2px; text-transform: uppercase; font-size: 11px; letter-spacing:0.3px;">' . htmlspecialchars($row['category_name']) . '</div>';
                    echo '<div style="color: #ffffff; font-weight: 600;">' . htmlspecialchars($row['service_name']) . '</div>';
                echo '</div>';
                
                // Footer Grid Spec Details
                echo '<div style="display: flex; justify-content: space-between; align-items: center; background: rgba(0,0,0,0.15); padding: 5px 10px; border-radius: 6px; font-size: 11px; margin-top: 2px;">';
                    echo '<span style="color: #94a3b8;">Min/Max Limits:</span>';
                    echo '<b style="color: #00f0ff; letter-spacing:0.5px;">' . number_format($row['service_min']) . ' / ' . number_format($row['service_max']) . '</b>';
                echo '</div>';

            echo '</div>';
        }
        echo '</div>';
    }
    exit;
}
?>