<?php

if (!function_exists('set_meta')) {
    function set_meta(string $title, string $description): array
    {
        return [
            'meta_title' => esc($title),
            'meta_desc' => esc($description)
        ];
    }
}

if (!function_exists('setting')) {
    function setting(string $key, $default = null)
    {
        static $settings = null;
        if ($settings === null) {
            $model = new \App\Models\SettingModel();
            $all = $model->findAll();
            $settings = [];
            foreach ($all as $s) {
                $settings[$s['key']] = $s['value'];
            }
        }
        return $settings[$key] ?? $default;
    }
}

if (!function_exists('schema_travel_agency')) {
    function schema_travel_agency(): string
    {
        $schema = [
            '@context' => 'https://schema.org',
            '@type' => 'TravelAgency',
            'name' => 'Mount Kenya Treks and Safaris',
            'url' => base_url(),
            'logo' => base_url('assets/images/logo.svg'),
            'address' => [
                '@type' => 'PostalAddress',
                'addressLocality' => 'Nanyuki',
                'addressCountry' => 'KE'
            ],
            'geo' => [
                '@type' => 'GeoCoordinates',
                'latitude' => -0.3031,
                'longitude' => 37.0786
            ],
            'telephone' => '+254700123456',
            'sameAs' => [
                'https://www.facebook.com/mountkenyatreks',
                'https://www.instagram.com/mountkenyatreks'
            ]
        ];
        return '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES) . '</script>';
    }
}

if (!function_exists('schema_tour')) {
    function schema_tour(array $tour): string
    {
        $schema = [
            '@context' => 'https://schema.org',
            '@type' => 'TouristAttraction',
            'name' => esc($tour['title']),
            'description' => esc($tour['description']),
            'url' => base_url("tours/{$tour['slug']}"),
            'offers' => [
                '@type' => 'Offer',
                'price' => $tour['price_usd'],
                'priceCurrency' => 'USD'
            ],
            'duration' => 'P' . $tour['duration_days'] . 'D',
            'image' => $tour['images'] ? base_url('assets/uploads/tours/' . json_decode($tour['images'], true)[0]) : null
        ];
        return '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES) . '</script>';
    }
}