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

// Ensure session is started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Optional: protect with admin login
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    die('You must be logged in as admin to run this script.');
}

$conn = get_db_connection();

// Make sure tables exist (same structure used in service-categories.php / services.php)
$conn->query("CREATE TABLE IF NOT EXISTS `service_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
  `description` text DEFAULT NULL,
  `display_order` int(11) DEFAULT 0,
  `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 `display_order` (`display_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

$conn->query("CREATE TABLE IF NOT EXISTS `services` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
  `description` text DEFAULT NULL,
  `content` longtext DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `display_order` int(11) DEFAULT 0,
  `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 `category_id` (`category_id`),
  KEY `display_order` (`display_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

$name = 'Chimney Animal Removal';
$slug = 'chimney-animal-removal';
$url = '/our-services/chimney-animal-removal/';

// Short description (first paragraph)
$description = "Are you tired of having an annoying smell in chimney pipes due to any dead animal? "
    . "Or have you noticed the presence of a bird or any other animal in the chimney pipe? "
    . "Then never ignore it – animals or birds living there can block air and push nasty-smelling gases into your home.";

// Main HTML content copied from the existing Elementor page
$content = '
<p>Are you tired of having an annoying smell in chimney pipes due to any
dead animal? Or have you noticed the presence of a bird or any other
animal in the chimney pipe? Then never ignore it. Because it could
be very dangerous for them to live there. They can grab on fire, go
into your home, or both so they need to be relieved from chimneys on
an urgent basis. Animals or fowls living there can cause obstacle
for air to pass through it and push the nasty-smelling gases into
your home rather than permit them to vent into the air easily.</p>
<h2><strong>What is animal removal?</strong></h2>
<p>In warmer climates like Florida and during warmer seasons in states,
animals frequently venture into stink pipes via the plumbing pipe on
the roof, making it their home. Raccoons and a bird typically named
as the ‘chimney swift’ will simply climb in and out of chimneys,
creating an enticing place for them to bring up their young. It is
dangerous to have animals living inside the chimney; since raccoons
and chimney swifts return to the same chimney they have inhabited
before; at the time of removal our chimney sweeps will install a
chimney cap or similar animal guard to obviate those species from
reoccurring. When the raccoon realizes that it cannot come back into
the chimney to live in peacefully, then it’ll try to find a brand
new chimney to live comfortably in. Don’t let it be yours!</p>
<h2><strong>Who we are?</strong></h2>
<p>Crest Chimney animal removal performs all chimney services from the
annual sweep and inspection to full chimney rebuilds and new
installations along with animal removal. Professional licensed and
certified chimney sweeps are on staff to get the job done right.</p>
<p>Our qualified and professional masons have the skills and knowledge
to complete any masonry work from construction to repair of any
structure or the chimney bricks repair.</p>
<p>We also offer a full line of chimney repair and construction work,
providing great services for several years. All the installation and
repair services are performed by our highly qualified professionals.
We offer a few number of warranties on most of our commodities and
services and guarantee all workforce.</p>
<h2><strong>Why choose us?</strong></h2>
<p>Crest chimney animal removal professionals are trained to properly
perform chimney installation and other superior chimney services for
both home and industrial use. We bring and mount the highest quality
chimney supplies to guarantee the most appropriate function and
working of our customers’ chimneys. This help maintains a chimney’s
integrity and can often help reduce an individual’s energy costs as
compared to other chimney experts who deceive you in terms of
quality. We perform the easiest and most convenient animal removal
service from chimneys without giving any harm to the animal. Some
chimney services that we provide include chimney dampers, chimney
liner repair, chimney animal removal, chase cover installation,
chimney flashing, and chimney exhaust fans installation. Our Chimney
Safety certified professional chimney sweeps can also perform
guaranteed chimney inspection and cleaning services that could
benefit you in many ways. We believe in strong customer
relationships and gain their trust by providing quality services in
the least possible time and with supreme reliability.</p>
<h2><strong>Contact us</strong></h2>
<p>Whenever you feel the need for repair work in the chimney or you just
need to remove any animal stuck in your chimney then call Crest chimney
animal removal service to instantly get the animal out of it and
relieve them. We are highly concerned about animal’s life and
genuinely care for their safety therefore we have established a
complete team for animal removal from a chimney. You must also take
action and call us on the very first basis as soon as you discover
the presence of any animal in your chimney.</p>
';

// Check if service already exists
$check_stmt = $conn->prepare("SELECT id FROM services WHERE slug = ? LIMIT 1");
$check_stmt->bind_param("s", $slug);
$check_stmt->execute();
$check_result = $check_stmt->get_result();

if ($check_result && $check_result->num_rows > 0) {
    $row = $check_result->fetch_assoc();
    $check_stmt->close();
    echo "<p>Service already exists with ID: " . (int)$row['id'] . "</p>";
    echo "<p>You can view it at: <a href=\"" . BASE_URL . "/service.php?slug=" . htmlspecialchars($slug) . "\" target=\"_blank\">" . BASE_URL . "/service.php?slug=" . htmlspecialchars($slug) . "</a></p>";
    exit;
}
$check_stmt->close();

// Determine display order (append to end)
$order_result = $conn->query("SELECT COALESCE(MAX(display_order), 0) AS max_order FROM services");
$row = $order_result ? $order_result->fetch_assoc() : ['max_order' => 0];
$display_order = (int)$row['max_order'] + 1;

// Insert service (no category for now)
$stmt = $conn->prepare("INSERT INTO services (category_id, name, slug, description, content, url, display_order) VALUES (NULL, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi", $name, $slug, $description, $content, $url, $display_order);

if ($stmt->execute()) {
    $new_id = $stmt->insert_id;
    echo "<p>Service 'Chimney Animal Removal' added successfully with ID: " . (int)$new_id . "</p>";
    echo "<p>View on frontend: <a href=\"" . BASE_URL . "/service.php?slug=" . htmlspecialchars($slug) . "\" target=\"_blank\">" . BASE_URL . "/service.php?slug=" . htmlspecialchars($slug) . "</a></p>";
    echo "<p>View in admin: <a href=\"" . BASE_URL . "/admin/services\" target=\"_blank\">" . BASE_URL . "/admin/services</a></p>";
    echo "<p><strong>Note:</strong> After confirming it works, you can safely delete this file: <code>admin/add-chimney-animal-removal-service.php</code>.</p>";
} else {
    echo "<p>Error inserting service: " . htmlspecialchars($stmt->error ?: $conn->error) . "</p>";
}

$stmt->close();

