<?php

namespace App\Http\Controllers;

use App\Models\WebsiteSetting;
use Illuminate\Support\Facades\Schema;

class WebsiteController extends Controller
{
    private function settings(): array
    {
        static $cache = null;
        if ($cache === null) {
            if (!Schema::hasTable('website_settings')) {
                return [];
            }

            $cache = WebsiteSetting::where('is_public', true)->get()
                ->mapWithKeys(function ($setting) {
                    $value = match ($setting->type) {
                        'json' => json_decode($setting->value, true) ?: [],
                        'bool' => filter_var($setting->value, FILTER_VALIDATE_BOOLEAN),
                        'number' => is_numeric($setting->value) ? (float) $setting->value : $setting->value,
                        default => $setting->value,
                    };

                    return [$setting->key => $value];
                })
                ->toArray();
        }
        return $cache;
    }

    public function home()
    {
        return view('website.home', ['s' => $this->settings()]);
    }

    public function privacy()
    {
        return view('website.privacy', ['s' => $this->settings()]);
    }

    public function terms()
    {
        return view('website.terms', ['s' => $this->settings()]);
    }

    public function contact()
    {
        return view('website.contact', ['s' => $this->settings()]);
    }
}
