<?php
declare(strict_types=1);

$db = new SQLite3(__DIR__ . '/../data/services.sqlite', SQLITE3_OPEN_READONLY);
$db->busyTimeout(3000);
$svc = $db->querySingle("SELECT * FROM services WHERE slug='" . SQLite3::escapeString($slug) . "'", true);
$db->close();

if (!$svc) {
    http_response_code(404);
    $page_title = '404: Service Not Found | Floor It Hardwood Floors';
    $page_meta  = '';
    require __DIR__ . '/../components/_header.php';
    echo '<section class="section section--light"><div class="container"><h1>Service Not Found</h1><p><a href="/services/">View all services</a></p></div></section>';
    require __DIR__ . '/../components/_footer.php';
    exit;
}

$page_title = $svc['title'];
$page_meta  = $svc['meta_description'];
$body       = json_decode($svc['body_json'] ?? '{}', true) ?? [];
$faqs       = json_decode($svc['faqs_json'] ?? '[]', true) ?? [];

$schema_arr = [
    '@context' => 'https://schema.org',
    '@type'    => 'Service',
    'name'     => $svc['title'],
    'description' => $svc['meta_description'],
    'provider' => [
        '@type'    => 'HomeAndConstructionBusiness',
        'name'     => 'Floor It Hardwood Floors',
        'url'      => 'https://floorithardwoodfloors.com',
        'telephone'=> '+17166021429',
    ],
    'areaServed' => 'Buffalo, NY',
];
if (!empty($faqs)) {
    $schema_arr['@graph'] = [[
        '@type'      => 'FAQPage',
        'mainEntity' => array_map(fn($f) => [
            '@type'          => 'Question',
            'name'           => $f['q'],
            'acceptedAnswer' => ['@type' => 'Answer', 'text' => $f['a']],
        ], $faqs),
    ]];
}
$schema_json = json_encode($schema_arr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

require __DIR__ . '/../components/_header.php';
?>

<section class="page-hero section section--dark">
  <div class="container">
    <div class="page-hero-content">
      <span class="eyebrow"><?= htmlspecialchars($svc['hero_eyebrow'] ?? '', ENT_QUOTES) ?></span>
      <h1><?= htmlspecialchars($svc['hero_h1'] ?? '', ENT_QUOTES) ?></h1>
      <p class="lead"><?= htmlspecialchars($svc['hero_lead'] ?? '', ENT_QUOTES) ?></p>
      <a href="/contact/" class="btn btn--primary btn--lg">Get a Free Estimate</a>
    </div>
  </div>
</section>

<?php if (!empty($body['intro_h2'])): ?>
<section class="section section--light">
  <div class="container service-intro">
    <div class="service-intro-text">
      <h2><?= htmlspecialchars($body['intro_h2'], ENT_QUOTES) ?></h2>
      <?php if (!empty($body['intro_body_1'])): ?><p><?= htmlspecialchars($body['intro_body_1'], ENT_QUOTES) ?></p><?php endif; ?>
      <?php if (!empty($body['intro_body_2'])): ?><p><?= htmlspecialchars($body['intro_body_2'], ENT_QUOTES) ?></p><?php endif; ?>
    </div>
    <?php if (!empty($svc['hero_image'])): ?>
    <div class="service-intro-img"><img src="<?= htmlspecialchars($svc['hero_image'], ENT_QUOTES) ?>" alt="<?= htmlspecialchars($svc['service_name'] ?? '', ENT_QUOTES) ?>" loading="lazy"></div>
    <?php endif; ?>
  </div>
</section>
<?php endif; ?>

<?php if (!empty($body['process_intro'])): ?>
<section class="section section--dark">
  <div class="container">
    <div class="section-header section-header--center">
      <span class="eyebrow">Our Process</span>
      <h2>How We Do It</h2>
      <p class="lead"><?= htmlspecialchars($body['process_intro'], ENT_QUOTES) ?></p>
    </div>
    <div class="grid grid--3col process-grid">
      <?php for ($i = 1; $i <= 3; $i++): $title_key = "step_{$i}_title"; $body_key = "step_{$i}_body"; ?>
      <?php if (!empty($body[$title_key])): ?>
      <div class="process-step">
        <div class="process-num">0<?= $i ?></div>
        <h3><?= htmlspecialchars($body[$title_key], ENT_QUOTES) ?></h3>
        <p><?= htmlspecialchars($body[$body_key] ?? '', ENT_QUOTES) ?></p>
      </div>
      <?php endif; ?>
      <?php endfor; ?>
    </div>
  </div>
</section>
<?php endif; ?>

<?php
$benefits = [];
for ($i = 1; $i <= 4; $i++) {
    if (!empty($body["benefit_{$i}_title"])) {
        $benefits[] = ['title' => $body["benefit_{$i}_title"], 'body' => $body["benefit_{$i}_body"] ?? ''];
    }
}
if ($benefits): ?>
<section class="section section--alt">
  <div class="container">
    <div class="section-header section-header--center">
      <span class="eyebrow">Why Choose Us</span>
      <h2>What Sets Us Apart</h2>
    </div>
    <div class="grid grid--4col benefits-grid">
      <?php foreach ($benefits as $b): ?>
      <div class="benefit-item">
        <h3><?= htmlspecialchars($b['title'], ENT_QUOTES) ?></h3>
        <p><?= htmlspecialchars($b['body'], ENT_QUOTES) ?></p>
      </div>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif; ?>

<?php if ($faqs): ?>
<section class="section section--light">
  <div class="container faq-section">
    <div class="section-header">
      <span class="eyebrow">Common Questions</span>
      <h2>Frequently Asked Questions</h2>
    </div>
    <div class="faq-list">
      <?php foreach ($faqs as $faq): ?>
      <details class="faq-item">
        <summary><?= htmlspecialchars($faq['q'] ?? '', ENT_QUOTES) ?></summary>
        <p><?= htmlspecialchars($faq['a'] ?? '', ENT_QUOTES) ?></p>
      </details>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif; ?>

<section class="section section--amber cta-section">
  <div class="container cta-section-inner">
    <div class="cta-section-text">
      <h2><?= htmlspecialchars($body['form_h2'] ?? 'Get Your Free Estimate', ENT_QUOTES) ?></h2>
      <p>Contact our team and we will respond within 24 hours.</p>
    </div>
    <a href="/contact/" class="btn btn--primary btn--lg"><?= htmlspecialchars($body['form_submit'] ?? 'Request Estimate', ENT_QUOTES) ?></a>
  </div>
</section>

<?php require __DIR__ . '/../components/_footer.php'; ?>
