<?php
/**
 * Import FAQs from hardcoded HTML to database
 * Run this script once to populate the database with existing FAQs
 */

require_once '../config.php';

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

// Check if user is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    die('Access denied. Please login first.');
}

$conn = get_db_connection();

// Create table if it doesn't exist
$conn->query("CREATE TABLE IF NOT EXISTS `faqs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question` text NOT NULL,
  `answer` text NOT 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`),
  KEY `display_order` (`display_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

// Check if FAQs already exist
$result = $conn->query("SELECT COUNT(*) as count FROM faqs");
$row = $result->fetch_assoc();
if ($row['count'] > 0) {
    echo "FAQs already exist in database. Skipping import.<br>";
    echo "<a href='faq'>Go to FAQ Management</a>";
    exit;
}

// FAQ data extracted from the HTML
$faqs = [
    [
        'question' => 'How often should I get my chimney swept?',
        'answer' => '<p>Good question. NACS recommend the following:</p><ul><li>Smokeless fuel – once a year</li><li>Gas &amp; Oil – once a year</li><li>Bituminous coal – twice a year</li><li>Wood – once a quarter when in use</li></ul><p>However, in our experience no two fires are the same. The quality of fuel and duration of use make a huge difference. We can advise you after sweeping.</p>',
        'display_order' => 1
    ],
    [
        'question' => 'What are signs of a dirty chimney?',
        'answer' => '<p>One of the easiest&nbsp;ways to diagnose a dirty chimney&nbsp;is with a simple inspection. Look up the length of the chimney lining towards the damper, taking notice of any material buildup more than 1/4 to 1/8 of an inch thick.</p><p>While every chimney has build-up, the amount changes depending on the type of fireplace. Wood fireplaces can also have different levels of buildup based on the type of wood being burned. &nbsp;</p><p>Another easy way to check for material buildup is by opening or closing the damper. Heavy buildup should loosen and fall off, signaling it\'s time for a chimney sweep.&nbsp;</p><p>Lastly, use good judgment! The more you use your fireplace, the more often it needs to be cleaned.</p>',
        'display_order' => 2
    ],
    [
        'question' => 'How much does it cost?',
        'answer' => '<p>Usually it depend on your location and work required. Large inglenooks can cost slightly more, as can removal of bird nests.</p>',
        'display_order' => 3
    ],
    [
        'question' => 'Will there be any mess?',
        'answer' => '<p>We sincerely hope not. We take the greatest care to ensure that all soot and dirt is contained. We have swept thousands of chimneys over the years and not had a single complaint.</p>',
        'display_order' => 4
    ],
    [
        'question' => 'Why should I hire a NACS registered sweep?',
        'answer' => '<p>Most insurance companies will no longer pay out for house fire claims caused by chimney fires unless the&nbsp;chimney has been swept by a trained professional chimney sweep.</p><p>All NACS sweeps are trained and fully insured. You can be sure your chimney will be swept to the highest standard.</p>',
        'display_order' => 5
    ],
    [
        'question' => 'Do you send small children up the chimney?',
        'answer' => '<p>No.</p>',
        'display_order' => 6
    ],
    [
        'question' => 'Have you ever considered your fireplace as a fire hazard?',
        'answer' => '<p>It sounds like an oxymoron. That is what a fireplace is for: a place for fire.&nbsp; But a dirty fireplace, that\'s asking for trouble.</p><ul><li>Your fireplace releases gases into the air which then travel up the chimney.</li><li>Condensation turns these gases into a thick combustible residue, also known as&nbsp;Creosote.</li><li>When your chimney lining is coated in this substance, you run the risk of a house fire.&nbsp;</li><li>So, have regular chimney cleaning performed by professionals to avoid Creosote buildup.</li></ul>',
        'display_order' => 7
    ],
    [
        'question' => 'What are some common phrases or terms to understand chimney?',
        'answer' => '<p><strong>Firebox:</strong> Fireplace interior walls. Include the visible area of the fireplace inside the house. Firebox can be manufacture out of brick or fabricated (brick design on a metal or ceramic wall panels).</p><p><strong>Flue:</strong> A metal duct connecting the firebox with the chimney cap at the top of the chimney structure. The flue will vent smoke rising from the fireplace to the exterior. Flue build up is the most common reason for chimney fires which can easily transform into a house fire.</p><p><strong>Damper:</strong> Manual flop installed on a metal plate at the top part of the firebox. The flop can be used to open and close the flue allowing air flow in &amp; out of the house. The flop is usually left open unless the fireplace is off and then it can be closed in order to prevent cold air from entering the house.</p><p><strong>Chimney Cap:</strong> The chimney cap is installed on the top of the flue at the top of the exterior chimney structure, it prevents rain water from accessing the flue and fireplace which can cause water damage to the house and corrosion to the metal parts of the chimney, there are custom chimney caps which are usually rectangular and covering the whole chimney structure top, basic chimney caps will usually be round or square in shape and will only cover the flue top.</p><p><strong>Creosote:</strong> A product of burning wood and coal, the creosote buildup in the chimney is the main cause of chimney fires. This build up might ignite when the fire is burning in your fireplace, it might go undetected from inside the house but it might also cause a full blown house fire. You might have a fire in your chimney and not be aware of it until it\'s too late. Atlanta chimney cleaning offers annual chimney cleaning also known as chimney sweep which will remove the dangerous levels of creosote buildup in the chimney.</p><p><strong>Chimney Cap Screen:</strong> This screen will prevent birds from nesting in the flue, squirrels from falling in to the fireplace, and other rodents from gaining access. (Warning-might delay Santa clause from accessing your house). Atlanta chimney cleaning offers installation of screens on you existing chimney caps.</p><p><strong>Wood Burning Stove:</strong> Standalone metal stove with its own flue or Metal insert installed inside the fireplace and uses the existing chimney flue. It used to burns wood logs. Cleaning process is usually more intense and requires extra time.</p>',
        'display_order' => 8
    ],
    [
        'question' => 'Why you need to hire a professional for chimney cleaning?',
        'answer' => '<p>Having your chimney cleaned by a professional gives you the opportunity to have the chimney construction and condition examined.</p><ul><li>Regular use, cracks, lightning, and moisture&nbsp;are just a few of the things that can affect the structural integrity of your chimney.</li><li>During a&nbsp;chimney cleaning, most chimney sweepers will also examine the rooftop, firebox, and foundation.</li><li>This will also ensure that your chimney is not housing birds and other pests.</li></ul>',
        'display_order' => 9
    ]
];

// Insert FAQs
$stmt = $conn->prepare("INSERT INTO faqs (question, answer, display_order) VALUES (?, ?, ?)");
$inserted = 0;
$errors = 0;

foreach ($faqs as $faq) {
    $stmt->bind_param("ssi", $faq['question'], $faq['answer'], $faq['display_order']);
    if ($stmt->execute()) {
        $inserted++;
    } else {
        $errors++;
        echo "Error inserting FAQ: " . $stmt->error . "<br>";
    }
}

$stmt->close();

echo "<h2>FAQ Import Complete</h2>";
echo "<p>Successfully imported: $inserted FAQs</p>";
if ($errors > 0) {
    echo "<p>Errors: $errors</p>";
}
echo "<p><a href='faq'>Go to FAQ Management</a></p>";
?>
