<?php
// Include the database config
$db_file = __DIR__ . '/../config/db.php';

// Detect if running from admin directory or root
if (!file_exists($db_file)) {
    // Check one level up in case we are in admin/includes/ or similar
    $db_file_up = __DIR__ . '/../../config/db.php';
    if (file_exists($db_file_up)) {
        $db_file = $db_file_up;
    } else {
        // Database not configured, check where setup.php is
        $setup_url = (strpos($_SERVER['REQUEST_URI'], '/admin/') !== false) ? '../setup.php' : 'setup.php';
        header("Location: $setup_url");
        exit;
    }
}

require_once $db_file;

// Fetch settings
try {
    $stmt = $pdo->prepare("SELECT * FROM settings LIMIT 1");
    $stmt->execute();
    $settings = $stmt->fetch();
    
    if (!$settings) {
        $settings = [
            'company_name' => 'PT Namora Nexa Niaga',
            'tagline' => 'Premium Natural Stone Supplier',
            'email' => 'info@namoranexa.com',
            'phone' => '+62 812-3456-7890',
            'address' => 'Jl. Raya Batu Alam No. 88, Jakarta Selatan, Indonesia',
            'about_short' => 'PT Namora Nexa Niaga adalah penyedia dan distributor batu alam premium pilihan.',
            'about_full' => 'PT Namora Nexa Niaga...',
            'vision' => 'Menjadi penyedia batu alam premium terkemuka...',
            'mission' => 'Menyediakan batu alam berkualitas...',
            'whatsapp_number' => '6281234567890',
            'instagram' => '',
            'facebook' => '',
            'map_iframe' => '',
            'hero_img_1' => 'hero_bg.jpg',
            'hero_img_2' => 'hero_bg2.jpg',
            'hero_img_3' => 'hero_bg3.jpg',
            'about_img_1' => 'about_img.jpg',
            'about_img_2' => 'about_img2.jpg',
            'menu_about_visible' => 1,
            'menu_products_visible' => 1,
            'menu_gallery_visible' => 1,
            'menu_contact_visible' => 1,
            'menu_blog_visible' => 1
        ];
    }
} catch (Exception $e) {
    $setup_url = (strpos($_SERVER['REQUEST_URI'], '/admin/') !== false) ? '../setup.php' : 'setup.php';
    header("Location: $setup_url");
    exit;
}

/**
 * Returns a specific, high-quality, relevant Unsplash fallback image URL
 * based on the product name and category.
 */
function get_product_fallback_image($category, $name = '') {
    $category = strtolower($category);
    $name = strtolower($name);
    
    // Default fallback (generic aggregates/stone heap)
    $url = 'https://images.unsplash.com/photo-1574689232449-062432a514d4?auto=format&fit=crop&w=600&q=80';
    
    if (strpos($category, 'split') !== false || strpos($name, 'split') !== false) {
        // Pile of crushed stones (Batu Split)
        $url = 'https://images.unsplash.com/photo-1578328819058-b69f3a3b0f6b?auto=format&fit=crop&w=600&q=80'; 
    } elseif (strpos($category, 'abu') !== false || strpos($name, 'abu') !== false) {
        // Black sand/dust pile (Abu Batu)
        $url = 'https://images.unsplash.com/photo-1509720074406-619085800315?auto=format&fit=crop&w=600&q=80';
    } elseif (strpos($category, 'screening') !== false || strpos($name, 'screening') !== false) {
        // Small pea gravel/screening stones
        $url = 'https://images.unsplash.com/photo-1546482502-6f9479b1897e?auto=format&fit=crop&w=600&q=80';
    } elseif (strpos($category, 'base') !== false || strpos($name, 'base') !== false) {
        // Road base / crushed aggregate mixture
        $url = 'https://images.unsplash.com/photo-1586528116311-ad8dd3c8310d?auto=format&fit=crop&w=600&q=80';
    } elseif (strpos($category, 'terrazzo') !== false || strpos($name, 'terrazo') !== false) {
        // Polished Terrazzo surface textures
        $url = 'https://images.unsplash.com/photo-1618220179428-22790b461013?auto=format&fit=crop&w=600&q=80';
    } elseif (strpos($category, 'alam') !== false || strpos($name, 'alam') !== false || strpos($category, 'andesit') !== false) {
        // Natural stone wall tiles / facade
        $url = 'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?auto=format&fit=crop&w=600&q=80';
    }
    
    return $url;
}

function get_article_fallback_image($slug = '') {
    $slug = strtolower($slug);
    if (strpos($slug, 'split') !== false) {
        return 'https://images.unsplash.com/photo-1541888946425-d81bb19240f5?auto=format&fit=crop&w=600&q=50';
    } elseif (strpos($slug, 'terrazzo') !== false) {
        return 'https://images.unsplash.com/photo-1618220179428-22790b461013?auto=format&fit=crop&w=600&q=50';
    } elseif (strpos($slug, 'pasir') !== false || strpos($slug, 'abu') !== false) {
        return 'https://images.unsplash.com/photo-1509720074406-619085800315?auto=format&fit=crop&w=600&q=50';
    }
    return 'https://images.unsplash.com/photo-1581094794329-c8112a89af12?auto=format&fit=crop&w=600&q=50';
}
?>
