<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: ../login.php");
    exit;
}
include '../../includes/db.php';

// =============================================================
//  FULL DEMO BLOG – ALL BLOCKS
//  "Future of AI in Steel Industry – Balimeen Pvt Ltd"
// =============================================================
$title = "Future of AI in Steel Industry – Balimeen Pvt Ltd";

$blocks = [
    // 1. HEADING – Main title
    [
        "type" => "header",
        "data" => ["text" => "Future of AI in Steel Industry – Balimeen Pvt Ltd", "level" => 2]
    ],
    // 2. PARAGRAPH
    [
        "type" => "paragraph",
        "data" => ["text" => "Artificial Intelligence (AI) is transforming the steel industry by improving efficiency, reducing waste, and enhancing production quality. Companies like <b>Balimeen Pvt Ltd</b> are adopting smart technologies to stay ahead in a competitive global market."]
    ],
    // 3. IMAGE
    [
        "type" => "image",
        "data" => [
            "file" => ["url" => "/bm/uploads/demo_steel_ai.jpg"],
            "caption" => "AI-powered steel manufacturing at Balimeen Pvt Ltd",
            "withBorder" => false,
            "stretched" => false,
            "withBackground" => false
        ]
    ],
    // 4. HEADING – Section
    [
        "type" => "header",
        "data" => ["text" => "Benefits of AI in the Steel Industry", "level" => 3]
    ],
    // 5. LIST
    [
        "type" => "list",
        "data" => [
            "style" => "unordered",
            "items" => [
                ["content" => "Automation in production lines", "items" => []],
                ["content" => "Predictive maintenance – reducing downtime by 40%", "items" => []],
                ["content" => "Cost reduction through smart energy usage", "items" => []],
                ["content" => "Improved worker safety with smart sensors", "items" => []],
                ["content" => "Real-time quality control using computer vision", "items" => []]
            ]
        ]
    ],
    // 6. QUOTE
    [
        "type" => "quote",
        "data" => [
            "text" => "AI is the backbone of future industrial growth. Those who adopt it today will lead the world tomorrow.",
            "caption" => "– CEO, Balimeen Pvt Ltd",
            "alignment" => "left"
        ]
    ],
    // 7. CODE
    [
        "type" => "code",
        "data" => ["code" => "def steel_quality_check(sensor_data):\n    \"\"\"\n    AI-based quality check for steel products.\n    Returns quality classification based on sensor input.\n    \"\"\"\n    if sensor_data['temperature'] > 1500 and sensor_data['purity'] > 90:\n        return 'High Quality Steel'\n    elif sensor_data['purity'] > 75:\n        return 'Standard Grade'\n    else:\n        return 'Reject – Re-process Required'\n\n# Sample sensor data\nresult = steel_quality_check({'temperature': 1520, 'purity': 92})\nprint(result)  # Output: High Quality Steel"]
    ],
    // 8. TABLE
    [
        "type" => "table",
        "data" => [
            "withHeadings" => true,
            "content" => [
                ["Process / Metric", "Before AI", "After AI", "Improvement"],
                ["Production Speed",   "Slow (manual)", "Fast (automated)", "+60% throughput"],
                ["Defect Rate",        "High (8-10%)",  "Low (1-2%)",       "80% reduction"],
                ["Energy Consumption", "Unoptimised",   "Smart-optimised",  "25% savings"],
                ["Maintenance Alerts", "Reactive",      "Predictive",       "40% less downtime"],
                ["Safety Incidents",   "Frequent",      "Rare",             "90% improvement"]
            ]
        ]
    ],
    // 9. DELIMITER
    [
        "type" => "delimiter",
        "data" => new stdClass()
    ],
    // 10. PREFORMATTED
    [
        "type" => "preformatted",
        "data" => ["text" => "=== BALIMEEN STEEL PLANT – SHIFT REPORT ===\nDate     : 27-Mar-2026\nShift    : Morning (06:00 – 14:00)\nProduction:\n  Billets  :  450 MT\n  Rods     :  320 MT\n  Sheets   :  180 MT\nQuality  : 98.4% pass rate\nAI Alert : None\n==========================================="]
    ],
    // 11. PULLQUOTE
    [
        "type" => "pullquote",
        "data" => [
            "text" => "Technology is not optional anymore — it is the foundation of survival in modern industry.",
            "citation" => "Balimeen Innovation Report 2026"
        ]
    ],
    // 12. VERSE
    [
        "type" => "verse",
        "data" => ["text" => "Steel flows like a river,\n    Machines hum forever.\nFrom ore to the sky,\n    Balimeen builds high."]
    ],
    // 13. DETAILS
    [
        "type" => "details",
        "data" => [
            "summary" => "Click to Learn: How AI Monitors Steel Quality",
            "content" => "AI uses a combination of infrared sensors, computer vision cameras, and machine learning models trained on thousands of steel samples. The system detects microscopic cracks, uneven thickness, and impurity levels in real-time — far more accurately than manual inspection. At Balimeen, this has reduced rejection rates from 8% to under 1.5%."
        ]
    ],
    // 14. CLASSIC
    [
        "type" => "classic",
        "data" => ["title" => "Classic Block", "desc" => "Rich text editor (TinyMCE / Classic Editor placeholder)"]
    ],
    // 15. BUTTON
    [
        "type" => "button",
        "data" => ["text" => "Explore Our Steel Services", "url" => "/bm/services.php", "style" => "primary"]
    ],
    // 16. SEPARATOR
    [
        "type" => "separator",
        "data" => new stdClass()
    ],
    // 17. SPACER
    [
        "type" => "spacer",
        "data" => ["height" => 50]
    ],
    // 18. HEADING – Widgets Demo
    [
        "type" => "header",
        "data" => ["text" => "Industry Insights & Widgets", "level" => 3]
    ],
    // 19. SEARCH WIDGET
    [
        "type" => "search",
        "data" => ["title" => "Search", "desc" => "Search blog content"]
    ],
    // 20. LATEST POSTS
    [
        "type" => "latest-posts",
        "data" => ["title" => "Latest Posts", "desc" => "5 most recent articles"]
    ],
    // 21. CATEGORIES LIST
    [
        "type" => "categories",
        "data" => ["title" => "Categories List", "desc" => "Steel, Technology, Business"]
    ],
    // 22. TAG CLOUD
    [
        "type" => "tag-cloud",
        "data" => ["title" => "Tag Cloud", "desc" => "AI, Steel, Industry, Automation, Innovation"]
    ],
    // 23. RSS
    [
        "type" => "rss",
        "data" => ["title" => "RSS", "desc" => "Subscribe to our feed"]
    ],
    // 24. SHORTCODE / CUSTOM HTML
    [
        "type" => "shortcode",
        "data" => ["code" => "[balimeen_contact_form id='1' title='Get a Free Quote']"]
    ],
    // 25. SOCIAL ICONS
    [
        "type" => "social",
        "data" => ["title" => "Social Icons", "desc" => "Facebook | Instagram | YouTube | LinkedIn"]
    ],
    // 26. ARCHIVES
    [
        "type" => "archives",
        "data" => ["title" => "Archives", "desc" => "Monthly archive of posts"]
    ],
    // 27. CALENDAR
    [
        "type" => "calendar",
        "data" => ["title" => "Calendar", "desc" => "March 2026"]
    ],
    // 28. LATEST COMMENTS
    [
        "type" => "latest-comments",
        "data" => ["title" => "Latest Comments", "desc" => "Recent comments on articles"]
    ],
    // 29. PAGE LIST
    [
        "type" => "page-list",
        "data" => ["title" => "Page List", "desc" => "Home, About, Services, Blogs, Contact"]
    ],
    // 30. TERMS LIST
    [
        "type" => "terms-list",
        "data" => ["title" => "Terms List", "desc" => "Steel Manufacturing, AI Automation, Quality Control"]
    ],
    // 31. COLUMNS
    [
        "type" => "columns",
        "data" => ["title" => "Columns", "desc" => "Col 1: AI Overview | Col 2: Steel Process Image"]
    ],
    // 32. GROUP
    [
        "type" => "group",
        "data" => ["title" => "Group", "desc" => "Grouped: Text + Image about Balimeen plant"]
    ],
    // 33. ROW
    [
        "type" => "row",
        "data" => ["title" => "Row", "desc" => "Horizontal: Logo | Title | CTA Button"]
    ],
    // 34. STACK
    [
        "type" => "stack",
        "data" => ["title" => "Stack", "desc" => "Vertical: Heading → Paragraph → Button"]
    ],
    // 35. GRID
    [
        "type" => "grid",
        "data" => ["title" => "Grid", "desc" => "2×2: Factory | Robot | Worker | Dashboard"]
    ],
    // 36. MORE (Read More separator)
    [
        "type" => "more",
        "data" => ["title" => "More", "desc" => "Read More separator for excerpt"]
    ],
    // ---- THEME BLOCKS ----
    // 37. NAVIGATION
    [
        "type" => "navigation",
        "data" => ["title" => "Navigation", "desc" => "Home | About | Services | Blogs | Contact"]
    ],
    // 38. SITE LOGO
    [
        "type" => "site-logo",
        "data" => ["title" => "Site Logo", "desc" => "Balimeen Pvt Ltd logo"]
    ],
    // 39. SITE TITLE
    [
        "type" => "site-title",
        "data" => ["title" => "Site Title", "desc" => "Balimeen Pvt Ltd"]
    ],
    // 40. SITE TAGLINE
    [
        "type" => "site-tagline",
        "data" => ["title" => "Site Tagline", "desc" => "Quality Steel, Smart Future"]
    ],
    // 41. QUERY LOOP
    [
        "type" => "query-loop",
        "data" => ["title" => "Query Loop", "desc" => "Loop: Latest 6 blog posts"]
    ],
    // 42. AVATAR
    [
        "type" => "avatar",
        "data" => ["title" => "Avatar", "desc" => "Raju Sharma"]
    ],
    // 43. POST TITLE
    [
        "type" => "post-title",
        "data" => ["title" => "Title", "desc" => "Future of AI in Steel Industry"]
    ],
    // 44. POST EXCERPT
    [
        "type" => "post-excerpt",
        "data" => ["title" => "Excerpt", "desc" => "AI is reshaping steel manufacturing globally..."]
    ],
    // 45. FEATURED IMAGE
    [
        "type" => "featured-image",
        "data" => ["title" => "Featured Image", "desc" => "Steel plant aerial photo"]
    ],
    // 46. AUTHOR
    [
        "type" => "author",
        "data" => ["title" => "Author", "desc" => "Raju Sharma – Chief Technology Officer"]
    ],
    // 47. AUTHOR NAME
    [
        "type" => "author-name",
        "data" => ["title" => "Author Name", "desc" => "Raju Sharma"]
    ],
    // 48. POST DATE
    [
        "type" => "post-date",
        "data" => ["title" => "Date", "desc" => "27 March 2026"]
    ],
    // 49. MODIFIED DATE
    [
        "type" => "modified-date",
        "data" => ["title" => "Modified Date", "desc" => "27 March 2026"]
    ],
    // 50. POST CATEGORIES
    [
        "type" => "post-categories",
        "data" => ["title" => "Categories", "desc" => "Business, Technology, Steel Industry"]
    ],
    // 51. POST TAGS
    [
        "type" => "post-tags",
        "data" => ["title" => "Tags", "desc" => "AI, Steel, Automation, Industry 4.0, Balimeen"]
    ],
    // ---- EMBEDS ----
    // 52. YOUTUBE
    [
        "type" => "embed-youtube",
        "data" => ["url" => "https://www.youtube.com/watch?v=RzkD_rTEBYs", "platform" => "YouTube"]
    ],
    // 53. TWITTER
    [
        "type" => "embed-twitter",
        "data" => ["url" => "https://twitter.com/elonmusk/status/1585841080431321088", "platform" => "Twitter"]
    ],
    // 54. WORDPRESS
    [
        "type" => "embed-wordpress",
        "data" => ["url" => "https://wordpress.org", "platform" => "WordPress"]
    ],
    // 55. SPOTIFY
    [
        "type" => "embed-spotify",
        "data" => ["url" => "https://open.spotify.com/episode/4rOoJ6Egrf8K2IrywzwOMk", "platform" => "Spotify"]
    ],
    // 56. SOUNDCLOUD
    [
        "type" => "embed-soundcloud",
        "data" => ["url" => "https://soundcloud.com/forss/flickermood", "platform" => "SoundCloud"]
    ],
    // 57. FLICKR
    [
        "type" => "embed-flickr",
        "data" => ["url" => "https://www.flickr.com/photos/nasa", "platform" => "Flickr"]
    ],
    // 58. VIMEO
    [
        "type" => "embed-vimeo",
        "data" => ["url" => "https://vimeo.com/148751763", "platform" => "Vimeo"]
    ],
    // 59. REDDIT
    [
        "type" => "embed-reddit",
        "data" => ["url" => "https://www.reddit.com/r/technology/", "platform" => "Reddit"]
    ],
    // 60. TIKTOK
    [
        "type" => "embed-tiktok",
        "data" => ["url" => "https://www.tiktok.com/@science", "platform" => "TikTok"]
    ],
    // 61. INSTAGRAM
    [
        "type" => "embed-instagram",
        "data" => ["url" => "https://www.instagram.com/p/CgkjR7hvWRl/", "platform" => "Instagram"]
    ],
    // 62. FACEBOOK
    [
        "type" => "embed-facebook",
        "data" => ["url" => "https://www.facebook.com/Meta", "platform" => "Facebook"]
    ],
    // 63. PINTEREST
    [
        "type" => "embed-pinterest",
        "data" => ["url" => "https://www.pinterest.com/pin/steel-manufacturing/", "platform" => "Pinterest"]
    ],
    // ---- GALLERY ----
    // 64. GALLERY (custom)
    [
        "type" => "gallery",
        "data" => ["title" => "Gallery", "desc" => "Steel Factory | Robot Arm | Workers | Control Panel | Finished Products"]
    ],
    // ---- AUDIO / VIDEO / COVER / FILE / MEDIA-TEXT ----
    // 65. AUDIO
    [
        "type" => "audio",
        "data" => ["title" => "Audio", "desc" => "Podcast: AI in Manufacturing – Episode 12"]
    ],
    // 66. COVER
    [
        "type" => "cover",
        "data" => ["title" => "Cover", "desc" => "Full-width hero: Balimeen Steel Plant at Sunset"]
    ],
    // 67. FILE
    [
        "type" => "file",
        "data" => ["url" => "/bm/uploads/balimeen_ai_report_2026.pdf", "text" => "Download: Balimeen AI Industry Report 2026 (PDF)"]
    ],
    // 68. MEDIA & TEXT
    [
        "type" => "media-text",
        "data" => ["title" => "Media & Text", "desc" => "Image Left: Steel Billet | Text Right: About our production process"]
    ],
    // 69. VIDEO
    [
        "type" => "video",
        "data" => ["title" => "Video", "desc" => "Company Overview Video – Balimeen Pvt Ltd 2026"]
    ],
    // ---- RAW HTML ----
    // 70. RAW / CUSTOM HTML
    [
        "type" => "raw",
        "data" => ["html" => "<div class=\"alert alert-info\" style=\"border-left:4px solid #0d6efd;padding:16px;background:#f0f6ff;border-radius:6px;\"><strong>📢 Balimeen Announcement:</strong> We are now accepting bulk steel orders for Q2 2026. <a href='/bm/contact.php' style='color:#0d6efd;font-weight:bold;'>Contact us today →</a></div>"]
    ],
    // ---- CLOSING PARAGRAPH ----
    [
        "type" => "paragraph",
        "data" => ["text" => "Balimeen Pvt Ltd continues to pioneer AI-driven steel manufacturing in India. With investments in smart sensors, computer vision, and predictive analytics, we are building the future of steel — block by block. <a href='/bm/contact.php'><b>Contact us</b></a> to learn how we can power your next infrastructure project."]
    ],
    // ---- FINAL SEPARATOR ----
    [
        "type" => "delimiter",
        "data" => new stdClass()
    ]
];

$content = json_encode(["time" => time() * 1000, "blocks" => $blocks, "version" => "2.29.0"], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$image_url = "/bm/uploads/demo_steel_ai.jpg";

// Check if demo post already exists
$check = $conn->prepare("SELECT id FROM blogs WHERE title = ?");
$check->bind_param("s", $title);
$check->execute();
$existing = $check->get_result()->fetch_assoc();
$check->close();

if ($existing) {
    // Update existing demo post
    $stmt = $conn->prepare("UPDATE blogs SET content = ?, image_url = ? WHERE id = ?");
    $stmt->bind_param("ssi", $content, $image_url, $existing['id']);
    $stmt->execute();
    $newId = $existing['id'];
    $stmt->close();
    $msg = "Demo blog UPDATED (ID: $newId)";
} else {
    // Insert new demo post
    $stmt = $conn->prepare("INSERT INTO blogs (title, content, image_url) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $title, $content, $image_url);
    $stmt->execute();
    $newId = $conn->insert_id;
    $stmt->close();
    $msg = "Demo blog CREATED (ID: $newId)";
}

// Redirect to the editor with this post loaded
header("Location: post.php?edit=$newId&type=blog&demo=1");
exit;
?>
