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

/**
 * Render <title>, meta description, canonical, Open Graph and Twitter Card tags.
 * Call before including header.php.
 */
function seo_meta(array $opts): string {
    $title = $opts['title'] ?? SITE_NAME;
    $description = $opts['description'] ?? SITE_TAGLINE;
    $path = $opts['path'] ?? current_path();
    $canonical = rtrim(SITE_URL, '/') . $path;
    $image = $opts['image'] ?? (rtrim(SITE_URL, '/') . '/assets/images/hero/home-hero-1.jpg');
    $type = $opts['type'] ?? 'website';

    $out = '';
    $out .= '<title>' . e($title) . '</title>' . "\n";
    $out .= '<meta name="description" content="' . e($description) . '">' . "\n";
    $out .= '<link rel="canonical" href="' . e($canonical) . '">' . "\n";
    $out .= '<meta property="og:site_name" content="' . e(SITE_NAME) . '">' . "\n";
    $out .= '<meta property="og:title" content="' . e($title) . '">' . "\n";
    $out .= '<meta property="og:description" content="' . e($description) . '">' . "\n";
    $out .= '<meta property="og:url" content="' . e($canonical) . '">' . "\n";
    $out .= '<meta property="og:type" content="' . e($type) . '">' . "\n";
    $out .= '<meta property="og:image" content="' . e($image) . '">' . "\n";
    $out .= '<meta property="og:locale" content="en_RW">' . "\n";
    $out .= '<meta name="twitter:card" content="summary_large_image">' . "\n";
    $out .= '<meta name="twitter:title" content="' . e($title) . '">' . "\n";
    $out .= '<meta name="twitter:description" content="' . e($description) . '">' . "\n";
    $out .= '<meta name="twitter:image" content="' . e($image) . '">' . "\n";
    return $out;
}

function json_ld(array $data): string {
    return '<script type="application/ld+json">' . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>' . "\n";
}

function schema_organization(): string {
    return json_ld([
        '@context' => 'https://schema.org',
        '@type' => 'RealEstateAgent',
        'name' => SITE_NAME,
        'image' => rtrim(SITE_URL, '/') . '/assets/images/hero/home-hero-1.jpg',
        'url' => SITE_URL,
        'telephone' => setting('phone', SITE_PHONE),
        'email' => setting('email', SITE_EMAIL),
        'address' => [
            '@type' => 'PostalAddress',
            'streetAddress' => setting('address', SITE_ADDRESS),
            'addressLocality' => SITE_CITY,
            'addressCountry' => 'RW',
        ],
        'areaServed' => 'Kigali, Rwanda',
        'sameAs' => [setting('facebook', SOCIAL_FACEBOOK), setting('instagram', SOCIAL_INSTAGRAM), setting('linkedin', SOCIAL_LINKEDIN), setting('twitter', SOCIAL_TWITTER), setting('youtube', SOCIAL_YOUTUBE)],
    ]);
}

function schema_local_business(): string {
    return json_ld([
        '@context' => 'https://schema.org',
        '@type' => 'LocalBusiness',
        'name' => SITE_NAME,
        'image' => rtrim(SITE_URL, '/') . '/assets/images/hero/home-hero-1.jpg',
        'telephone' => setting('phone', SITE_PHONE),
        'priceRange' => 'RWF',
        'address' => [
            '@type' => 'PostalAddress',
            'streetAddress' => setting('address', SITE_ADDRESS),
            'addressLocality' => SITE_CITY,
            'addressCountry' => 'RW',
        ],
        'openingHoursSpecification' => [
            '@type' => 'OpeningHoursSpecification',
            'dayOfWeek' => ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
            'opens' => '08:00',
            'closes' => '18:00',
        ],
    ]);
}

function schema_breadcrumb(array $items): string {
    $list = [];
    foreach ($items as $i => $item) {
        $list[] = [
            '@type' => 'ListItem',
            'position' => $i + 1,
            'name' => $item['name'],
            'item' => rtrim(SITE_URL, '/') . $item['url'],
        ];
    }
    return json_ld([
        '@context' => 'https://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => $list,
    ]);
}

function schema_faq(array $faqs): string {
    $items = array_map(fn($f) => [
        '@type' => 'Question',
        'name' => $f['question'],
        'acceptedAnswer' => ['@type' => 'Answer', 'text' => strip_tags($f['answer'])],
    ], $faqs);
    return json_ld([
        '@context' => 'https://schema.org',
        '@type' => 'FAQPage',
        'mainEntity' => $items,
    ]);
}

function schema_property(array $property): string {
    $img = get_property_primary_image((int)$property['id']);
    return json_ld([
        '@context' => 'https://schema.org',
        '@type' => 'Residence',
        'name' => $property['title'],
        'description' => $property['short_description'],
        'image' => $img ? rtrim(SITE_URL, '/') . '/' . $img : null,
        'address' => [
            '@type' => 'PostalAddress',
            'streetAddress' => $property['address'],
            'addressLocality' => SITE_CITY,
            'addressCountry' => 'RW',
        ],
        'numberOfRooms' => (int)$property['bedrooms'],
        'numberOfBathroomsTotal' => (int)$property['bathrooms'],
        'floorSize' => [
            '@type' => 'QuantitativeValue',
            'value' => (int)$property['building_size_sqm'],
            'unitCode' => 'MTK',
        ],
        'offers' => [
            '@type' => 'Offer',
            'price' => (float)$property['price'],
            'priceCurrency' => $property['currency'],
            'availability' => 'https://schema.org/InStock',
            'businessFunction' => $property['status'] === 'rent' ? 'https://schema.org/LeaseOut' : 'https://schema.org/Sell',
        ],
    ]);
}

function breadcrumb_html(array $items): string {
    $html = '<nav class="breadcrumbs" aria-label="Breadcrumb"><a href="/">Home</a>';
    foreach ($items as $i => $item) {
        $html .= '<span class="sep">/</span>';
        if ($i === count($items) - 1) {
            $html .= '<span aria-current="page">' . e($item['name']) . '</span>';
        } else {
            $html .= '<a href="' . e($item['url']) . '">' . e($item['name']) . '</a>';
        }
    }
    $html .= '</nav>';
    return $html;
}
