<?php
include 'includes/db.php';

// 1. Fix paths for existing services
// Old path likely: 'our servies image/...'
// New path target: 'our servies image/Our Services/...'
// We will update based on title to ensure we target correctly.

$updates = [
    'Development of IOCL Retail Outlet' => 'our servies image/Our Services/Petro-Pump-Retail-Outlet.jpeg',
    'Fabrication Works' => 'our servies image/Our Services/fabrication_wor.jpeg',
    'Development Of Road' => 'our servies image/Our Services/Development_road.jpeg',
    'Interior Work' => 'our servies image/Our Services/interior_work.jpeg',
    'Aluminium Work' => 'our servies image/Our Services/Aluminium_work.jpeg'
];

foreach ($updates as $title => $img) {
    $stmt = $conn->prepare("UPDATE services SET image_url = ? WHERE title = ?");
    $stmt->bind_param("ss", $img, $title);
    if ($stmt->execute()) {
        echo "Updated path for: $title <br>\n";
    } else {
        echo "Error updating $title: " . $conn->error . "<br>\n";
    }
}

// 2. Add New Services
// 'Construction_of_Building.jpeg'
// 'BPCL.jpeg'
// 'HPCL.jpeg'

$new_services = [
    [
        'title' => 'Building Construction',
        'desc' => 'Comprehensive building construction services ensuring stability and excellence.',
        'img' => 'our servies image/Our Services/Construction_of_Building.jpeg'
    ],
    [
        'title' => 'BPCL Retail Outlets',
        'desc' => 'Specialized development and setup for BPCL retail outlets.',
        'img' => 'our servies image/Our Services/BPCL.jpeg'
    ],
    [
        'title' => 'HPCL Projects',
        'desc' => 'Execution of HPCL infrastructure and retail projects.',
        'img' => 'our servies image/Our Services/HPCL.jpeg'
    ]
];

foreach ($new_services as $svc) {
    // Check if exists to avoid duplicates
    $check = $conn->prepare("SELECT id FROM services WHERE title = ?");
    $check->bind_param("s", $svc['title']);
    $check->execute();
    $res = $check->get_result();

    if ($res->num_rows == 0) {
        $stmt = $conn->prepare("INSERT INTO services (title, description, image_url) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $svc['title'], $svc['desc'], $svc['img']);
        if ($stmt->execute()) {
            echo "Added new service: " . $svc['title'] . "<br>\n";
        } else {
            echo "Error adding " . $svc['title'] . ": " . $conn->error . "<br>\n";
        }
    } else {
        echo "Service already exists: " . $svc['title'] . " (Skipping insert)<br>\n";
        // Optional: Update image if needed
        $stmt = $conn->prepare("UPDATE services SET image_url = ? WHERE title =?");
        $stmt->bind_param("ss", $svc['img'], $svc['title']);
        $stmt->execute();
        echo " - Updated image path just in case.<br>\n";
    }
}

echo "Done.";
?>
