<?php
/**
 * Auto-Populate Database with Aligned Products, Galleries, and Specifications
 * Path: populate-database.php
 */

require_once 'config.php';

header('Content-Type: text/html; charset=utf-8');

if (!$conn) {
    die("<div style='color: red; font-family: sans-serif; padding: 20px;'>
            <strong>ত্রুটি:</strong> ডাটাবেজ সংযোগ স্থাপন করা সম্ভব হয়নি।
         </div>");
}

$prefix = DB_PREFIX;

// --- ডাটাবেজ ক্লিয়ারেন্স (যাতে কোনো ডুপ্লিকেট কনফ্লিক্ট না হয়) ---
$conn->query("SET FOREIGN_KEY_CHECKS = 0;");
$conn->query("TRUNCATE TABLE `{$prefix}services`;");
$conn->query("TRUNCATE TABLE `{$prefix}product_images`;");
$conn->query("TRUNCATE TABLE `{$prefix}product_specs`;");
$conn->query("SET FOREIGN_KEY_CHECKS = 1;");

// --- সিড ডাটা প্রস্তুতকরণ ---
$seeding_data = [
    // PRODUCT 1: PASSENGER ELEVATOR
    [
        'title' => 'Premium Passenger Elevator (Lift)',
        'description' => "ISO-certified, high-speed passenger elevators engineered for modern commercial complexes and residential buildings in Dhaka, Bangladesh. Features highly efficient Gearless PM Synchronous traction machines, energy-saving VVVF AC controllers, safety-tested ARD systems, and elegant hairline stainless steel cabins equipped with intuitive infrared screen doors.",
        'image_url' => 'https://images.unsplash.com/photo-1518156677180-95a2893f3e9f?w=600',
        'slug' => 'passenger-cargo-elevators',
        'gallery' => [
            'https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?w=600',
            'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?w=600'
        ],
        'specs' => [
            'Equipment Type' => 'Passenger & Cargo Lift',
            'Capacity' => '8-Person to 20-Person (630 Kg to 1600 Kg)',
            'Rated Speed' => '1.0 m/s to 1.75 m/s',
            'Traction Engine' => 'Gearless Permanent Magnet Synchronous',
            'Control Card' => 'VVVF Dual 32-bit Microprocessor',
            'Door Structure' => 'Center Opening Automatic Sliding',
            'Safety Gears' => 'ARD (Automatic Rescue Device) & Door Multi-beams'
        ]
    ],
    // PRODUCT 2: STANDBY POWER GENERATOR
    [
        'title' => 'Standby Soundproof Diesel Generator (150 kVA)',
        'description' => "Heavy-duty soundproof industrial diesel generator designed to supply seamless automatic backup power for factories, hospitals, and corporate parks during unexpected power grid blackouts.",
        'image_url' => 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=600',
        'slug' => 'standby-diesel-generators',
        'gallery' => [
            'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=600',
            'https://images.unsplash.com/photo-1504307651254-35680f356dfd?w=600'
        ],
        'specs' => [
            'Standby Power' => '150 kVA / 120 kW',
            'Engine Brand' => 'Perkins (UK)',
            'Engine Model' => '1106A-70TAG2 (Turbocharged)',
            'Alternator' => 'Stamford (UK) UCI274F',
            'Fuel Type' => 'High-Speed Diesel',
            'Controller' => 'Deep Sea Electronics (DSE) 7310 Auto Mains Failure',
            'Canopy Type' => 'Super Soundproof Weatherproof (65 dBA @ 7m)'
        ]
    ],
    // PRODUCT 3: ELECTRICAL TRANSFORMER
    [
        'title' => 'HT Sub-station Oil-Immersed Transformer (500 kVA)',
        'description' => "Premium-grade, high-voltage distribution transformer optimized for stable industrial sub-stations across Bangladesh. Effectively steps down standard 11kV utility network grids to safe electrical loads required for operations.",
        'image_url' => 'https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?w=600',
        'slug' => 'ht-substation-transformers',
        'gallery' => [
            'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=600'
        ],
        'specs' => [
            'Rated Capacity' => '500 kVA',
            'Primary Voltage' => '11 kV',
            'Secondary Voltage' => '0.415 kV (415V/240V)',
            'Vector Group' => 'Dyn11',
            'Cooling Method' => 'ONAN (Oil Natural Air Natural)',
            'Standard compliant' => 'IEC 60076 / BSTI Certified',
            'Winding Type' => '99.9% Electrolytic Copper conductors'
        ]
    ],
    // PRODUCT 4: VRF CENTRAL AC
    [
        'title' => 'Centralized VRF HVAC Air Conditioning (20 HP)',
        'description' => "Highly efficient Variable Refrigerant Flow (VRF) climate comfort system engineered for corporate towers, shopping complexes, and clean-room factories in Bangladesh. Minimizes electric bills through smart DC Inverter Twin-Rotary scroll compressors.",
        'image_url' => 'https://images.unsplash.com/photo-1504307651254-35680f356dfd?w=600',
        'slug' => 'vrf-central-air-conditioning',
        'gallery' => [
            'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?w=600'
        ],
        'specs' => [
            'Total Capacity' => '20 HP / 56 kW Cooling',
            'Compressor' => 'DC Inverter Twin-Rotary/Scroll',
            'Refrigerant gas' => 'Eco-friendly R410A pre-charged',
            'COP (EER)' => '4.20 (Highly efficient class A)',
            'Indoor Connectivity' => 'Connects up to 64 diverse styled indoor units',
            'Pipe Length' => 'Total equivalent length up to 1,000 meters'
        ]
    ],
    // PRODUCT 5: CAR PARKING LIFT
    [
        'title' => 'Automated Hydraulic Car Parking Stacker (2,700 Kg)',
        'description' => "Heavy-duty space-saving hydraulic car parking lift designed to double residential garage and commercial basement capacities in congested cities. Features strict dynamic locks, high-strength chains, and manual override key-switch setups.",
        'image_url' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?w=600',
        'slug' => 'hydraulic-car-parking-lifts',
        'gallery' => [
            'https://images.unsplash.com/photo-1504307651254-35680f356dfd?w=600'
        ],
        'specs' => [
            'Lifting Weight Limit' => '2,700 Kg (Accommodates SUVs & Sedans)',
            'Clear Lift Height' => '2,100 mm',
            'Usable Platform Width' => '2,100 mm',
            'Control Mechanism' => 'Secure Key-Switch on dynamic stand',
            'Safety Locks' => 'Multi-stage fall-proof mechanical locking bars',
            'Motor Drive Unit' => 'High-pressure Hydraulic Power Pack'
        ]
    ]
];

$success_count = 0;

// --- ডাটাবেজে ডাটা পপুলেশন প্রসেস ---
foreach ($seeding_data as $p) {
    // ১. মেইন প্রোডাক্ট ইনসার্ট
    $stmt = $conn->prepare("INSERT INTO `{$prefix}services` (title, description, image_url, slug) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $p['title'], $p['description'], $p['image_url'], $p['slug']);
    
    if ($stmt->execute()) {
        $product_id = $conn->insert_id;
        $success_count++;
        
        // ২. মেইন প্রোডাক্টের সাথে গ্যালারি ইমেজ ইনসার্ট
        foreach ($p['gallery'] as $gal_img) {
            $stmt_gal = $conn->prepare("INSERT INTO `{$prefix}product_images` (product_id, image_url) VALUES (?, ?)");
            $stmt_gal->bind_param("is", $product_id, $gal_img);
            $stmt_gal->execute();
            $stmt_gal->close();
        }
        
        // ৩. মেইন প্রোডাক্টের সাথে স্পেসিফিকেশন ইনসার্ট
        foreach ($p['specs'] as $key => $val) {
            $stmt_sp = $conn->prepare("INSERT INTO `{$prefix}product_specs` (product_id, spec_key, spec_value) VALUES (?, ?, ?)");
            $stmt_sp->bind_param("iss", $product_id, $key, $val);
            $stmt_sp->execute();
            $stmt_sp->close();
        }
    }
    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>Database Seeder</title>
    <style>
        body { font-family: sans-serif; background: #0f172a; padding: 40px; color: #cbd5e1; text-align: center; }
        .container { max-width: 600px; background: #1e293b; padding: 30px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.3); margin: 0 auto; border: 1px solid #334155; }
        h2 { color: #38bdf8; border-bottom: 2px solid #334155; padding-bottom: 10px; }
        .success-box { background: #022c22; color: #4ade80; border: 1px solid #14532d; padding: 15px; border-radius: 8px; margin-top: 20px; font-weight: bold; }
        .btn { display: inline-block; background: #0ea5e9; color: #000; padding: 10px 20px; text-decoration: none; border-radius: 6px; font-weight: bold; margin-top: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h2>ডাইনামিক প্রোডাক্ট ও স্পেক্স সিডার স্ট্যাটাস</h2>
        <p>ডাটাবেজ আপগ্রেড সম্পন্ন হয়েছে এবং নতুন ৫টি প্রফেশনাল প্রোডাক্ট ডাটাবেজে সাকসেসফুলি ইনসার্ট করা হয়েছে।</p>
        <div style="margin: 20px 0; font-size: 14px; text-align: left; background: #0f172a; p: 15px; border-radius: 6px; padding: 15px;">
            <strong>নিশ্চিত করা ডাটা:</strong><br>
            - মোট প্রোডাক্ট ইনসার্ট: <?php echo $product_id ? $product_id : '0'; ?> টি<br>
            - আনলিমিটেড মাল্টি-ইমেজ গ্যালারি: <span style="color: #38bdf8;">সক্ষম</span><br>
            - ডাইনামিক স্পেক্স রিলেশন: <span style="color: #38bdf8;">সক্ষম</span>
        </div>
        <?php if ($product_id > 0): ?>
            <div class="success-box">ডাটাবেজ সম্পূর্ণভাবে ডাইনামিক ডাটা দ্বারা পূর্ণ হয়েছে!</div>
            <a href="index.php" class="btn">ওয়েবসাইটে যান</a>
        <?php else: ?>
            <p style="color: #f87171;">ডাটা পপুলেট করতে ব্যর্থ হয়েছে। অনুগ্রহ করে ডাটাবেজ টেবিল চেক করুন।</p>
        <?php endif; ?>
    </div>
</body>
</html>