<?php
require_once 'auth.php';

// Simple SEO Optimizer
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Database connection
$host = "localhost";
$user = "root";
$pass = "";
$db = "finggu";

$success_message = '';
$error_message = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    die('Database connection failed: ' . $e->getMessage());
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['optimize_seo'])) {
    try {
        $stmt = $pdo->query("
            SELECT cs.id, c.name as city_name, s.name as service_name
            FROM city_service cs
            JOIN cities c ON cs.city_id = c.id
            JOIN services s ON cs.service_id = s.id
            WHERE cs.seo_title IS NULL OR cs.seo_title = '' OR cs.seo_description IS NULL OR cs.seo_description = ''
            LIMIT 10
        ");
        $records = $stmt->fetchAll();
        
        $updateStmt = $pdo->prepare("UPDATE city_service SET seo_title = ?, seo_description = ? WHERE id = ?");
        $optimized = 0;
        
        foreach ($records as $record) {
            $cityName = $record['city_name'];
            $serviceName = $record['service_name'];
            
            $seoTitle = "$serviceName in $cityName | Finggu Infotech";
            $seoDescription = "Professional $serviceName services in $cityName. Contact Finggu Infotech for expert solutions.";
            
            $updateStmt->execute([$seoTitle, $seoDescription, $record['id']]);
            $optimized++;
        }
        
        $success_message = "✅ Optimized $optimized records!";
        
    } catch (Exception $e) {
        $error_message = "Error: " . $e->getMessage();
    }
}

// Get stats
$totalPages = $pdo->query("SELECT COUNT(*) FROM city_service")->fetchColumn();
$optimizedTitles = $pdo->query("SELECT COUNT(*) FROM city_service WHERE seo_title IS NOT NULL AND seo_title != ''")->fetchColumn();
$needsOptimization = $pdo->query("SELECT COUNT(*) FROM city_service WHERE seo_title IS NULL OR seo_title = ''")->fetchColumn();
?>
<!DOCTYPE html>
<html>
<head>
    <title>SEO Optimizer | Finggu Admin</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; }
        .container { max-width: 800px; margin: 0 auto; background: white; padding: 2rem; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); }
        .header { text-align: center; margin-bottom: 2rem; }
        .header h1 { color: #2c3e50; font-size: 2.5rem; margin-bottom: 0.5rem; }
        .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
        .stat-card { background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 1.5rem; border-radius: 10px; text-align: center; }
        .stat-value { font-size: 2rem; font-weight: bold; margin-bottom: 0.5rem; }
        .stat-label { font-size: 0.9rem; opacity: 0.9; }
        .message { padding: 1rem; border-radius: 8px; margin-bottom: 1rem; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .btn { background: linear-gradient(135deg, #2ecc71, #27ae60); color: white; padding: 1rem 2rem; border: none; border-radius: 8px; font-size: 1rem; cursor: pointer; }
        .btn:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,0,0,0.2); }
        .nav { margin-bottom: 2rem; }
        .nav a { color: #667eea; text-decoration: none; margin-right: 1rem; }
    </style>
</head>
<body>
    <div class="container">
        <div class="nav">
            <a href="modern-dashboard.php">← Back to Dashboard</a>
        </div>
        
        <div class="header">
            <h1>🚀 SEO Optimizer</h1>
            <p>Optimize your website's SEO performance</p>
        </div>

        <?php if ($success_message): ?>
            <div class="message success"><?= $success_message ?></div>
        <?php endif; ?>

        <?php if ($error_message): ?>
            <div class="message error"><?= $error_message ?></div>
        <?php endif; ?>

        <div class="stats">
            <div class="stat-card">
                <div class="stat-value"><?= $totalPages ?></div>
                <div class="stat-label">Total Pages</div>
            </div>
            <div class="stat-card">
                <div class="stat-value"><?= $optimizedTitles ?></div>
                <div class="stat-label">Optimized</div>
            </div>
            <div class="stat-card">
                <div class="stat-value"><?= $needsOptimization ?></div>
                <div class="stat-label">Needs Work</div>
            </div>
        </div>

        <div style="text-align: center;">
            <form method="POST">
                <button type="submit" name="optimize_seo" class="btn">
                    🔧 Optimize SEO (Next 10 Records)
                </button>
            </form>
        </div>

        <div style="margin-top: 2rem; text-align: center;">
            <p><a href="modern-seo-optimizer.php">Try Full SEO Optimizer</a></p>
        </div>
    </div>
</body>
</html>