<?php
require_once '../config.php';

// Create blogs table if it doesn't exist
$create_blogs_table = "CREATE TABLE IF NOT EXISTS `blogs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
  `content` longtext NOT NULL,
  `excerpt` text DEFAULT NULL,
  `featured_image` varchar(255) DEFAULT NULL,
  `category` varchar(100) DEFAULT 'general',
  `tags` text DEFAULT NULL,
  `status` enum('draft','published','archived') DEFAULT 'draft',
  `views` int(11) DEFAULT 0,
  `likes` int(11) DEFAULT 0,
  `author_id` int(11) DEFAULT 1,
  `meta_title` varchar(255) DEFAULT NULL,
  `meta_description` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug` (`slug`),
  KEY `status` (`status`),
  KEY `category` (`category`),
  KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

if (mysqli_query($link, $create_blogs_table)) {
    echo "✅ Blogs table created successfully\n";
} else {
    echo "❌ Error creating blogs table: " . mysqli_error($link) . "\n";
}

// Insert sample blog data using simple queries
$sample_blogs = [
    [
        'title' => 'Ultimate Satta Matka Guide 2024',
        'slug' => 'ultimate-satta-matka-guide-2024',
        'content' => '<h2>Complete Guide to Satta Matka</h2><p>Satta Matka is one of India most popular number prediction games. Learn the strategies, tips, and techniques to improve your gameplay.</p>',
        'excerpt' => 'Learn the complete guide to satta matka with expert tips and strategies for 2024',
        'category' => 'tips',
        'tags' => 'satta matka, guide, tips, strategies, 2024',
        'status' => 'published',
        'views' => 245,
        'likes' => 35
    ],
    [
        'title' => 'Kalyan Matka Winning Strategies',
        'slug' => 'kalyan-matka-winning-strategies',
        'content' => '<h2>Professional Kalyan Matka Strategies</h2><p>Kalyan is the king of all matka games. Master these proven strategies to improve your success rate.</p>',
        'excerpt' => 'Professional strategies and tips for winning at Kalyan matka games',
        'category' => 'strategies',
        'tags' => 'kalyan matka, strategies, winning tips, chart analysis',
        'status' => 'published',
        'views' => 189,
        'likes' => 28
    ],
    [
        'title' => 'Market Analysis and Trends 2024',
        'slug' => 'market-analysis-trends-2024',
        'content' => '<h2>Current Market Analysis</h2><p>Stay updated with the latest market trends and analysis for all major matka markets.</p>',
        'excerpt' => 'Detailed market analysis and current trends for all major matka markets in 2024',
        'category' => 'analysis',
        'tags' => 'market analysis, trends, 2024, matka markets',
        'status' => 'published',
        'views' => 156,
        'likes' => 22
    ]
];

foreach ($sample_blogs as $blog) {
    $title = mysqli_real_escape_string($link, $blog['title']);
    $slug = mysqli_real_escape_string($link, $blog['slug']);
    $content = mysqli_real_escape_string($link, $blog['content']);
    $excerpt = mysqli_real_escape_string($link, $blog['excerpt']);
    $category = mysqli_real_escape_string($link, $blog['category']);
    $tags = mysqli_real_escape_string($link, $blog['tags']);
    $status = mysqli_real_escape_string($link, $blog['status']);
    
    $query = "INSERT IGNORE INTO blogs (title, slug, content, excerpt, category, tags, status, views, likes) VALUES 
              ('$title', '$slug', '$content', '$excerpt', '$category', '$tags', '$status', {$blog['views']}, {$blog['likes']})";
    
    if (mysqli_query($link, $query)) {
        echo "✅ Inserted blog: " . $blog['title'] . "\n";
    } else {
        echo "❌ Error inserting blog: " . mysqli_error($link) . "\n";
    }
}

echo "\n🎉 Sample blog data setup complete!\n";
?>