<?php

namespace App\Support;

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

class Branding
{
    protected static ?array $cache = null;

    public static function all(): array
    {
        if (self::$cache !== null) {
            return self::$cache;
        }

        if (!Schema::hasTable('settings')) {
            return self::$cache = self::defaults();
        }

        $logoPath = Setting::get('site_logo');
        $faviconPath = Setting::get('site_favicon');

        self::$cache = [
            'company_name' => Setting::get('company_name', "MJ's Bikes & Accessories"),
            'company_name_nav' => Setting::get('company_name_nav', "MJ'S <span>BIKES</span>"),
            'company_tagline' => Setting::get('company_tagline', 'Premium bicycles, spare parts & repair services in Nairobi.'),
            'company_about' => Setting::get('company_about', "MJ's Bikes & Accessories is your primary showroom for high-quality bicycles, spares, and specialized mechanical repair services in Nairobi."),
            'about_content' => Setting::get('about_content', ''),
            'meta_description' => Setting::get('meta_description', "Discover premium road, mountain, and hybrid bicycles, high-quality spare parts, and book professional repair services at MJ's Bikes & Accessories Marketplace."),
            'copyright_text' => Setting::get('copyright_text', "© " . date('Y') . " MJ's Bikes & Accessories Marketplace. All rights reserved."),
            'business_hours' => Setting::get('business_hours', 'Mon - Sat: 8:00 AM - 6:30 PM'),
            'whatsapp_greeting' => Setting::get('whatsapp_greeting', "Hi MJ's Bikes, I'm inquiring about your showroom and repair services."),
            'site_logo' => $logoPath,
            'site_favicon' => $faviconPath,
            'logo_url' => self::assetIfExists($logoPath),
            'favicon_url' => self::assetIfExists($faviconPath),
            'footer_email' => Setting::get('footer_email', 'info@mjsbikes.com'),
            'footer_phone' => Setting::get('footer_phone', '+254 712 345 678'),
            'footer_address' => Setting::get('footer_address', 'Mombasa Road, Opp. Cabanas, Nairobi, Kenya'),
            'footer_facebook' => Setting::get('footer_facebook', 'https://facebook.com'),
            'footer_instagram' => Setting::get('footer_instagram', 'https://instagram.com'),
            'footer_whatsapp' => Setting::get('footer_whatsapp', '+254 712 345 678'),
            'whatsapp_clean' => self::cleanWhatsApp(Setting::get('footer_whatsapp', '+254 712 345 678')),
        ];

        return self::$cache;
    }

    public static function get(string $key, mixed $default = null): mixed
    {
        return self::all()[$key] ?? $default;
    }

    public static function pageTitle(?string $suffix = null): string
    {
        $name = self::get('company_name');

        return $suffix ? "{$suffix} - {$name}" : "{$name} Marketplace";
    }

    public static function flush(): void
    {
        self::$cache = null;
    }

    public static function defaults(): array
    {
        return [
            'company_name' => "MJ's Bikes & Accessories",
            'company_name_nav' => "MJ'S <span>BIKES</span>",
            'company_tagline' => 'Premium bicycles, spare parts & repair services in Nairobi.',
            'company_about' => "MJ's Bikes & Accessories is your primary showroom for high-quality bicycles, spares, and specialized mechanical repair services in Nairobi.",
            'about_content' => '',
            'meta_description' => "Discover premium road, mountain, and hybrid bicycles, high-quality spare parts, and book professional repair services at MJ's Bikes & Accessories Marketplace.",
            'copyright_text' => '© ' . date('Y') . " MJ's Bikes & Accessories Marketplace. All rights reserved.",
            'business_hours' => 'Mon - Sat: 8:00 AM - 6:30 PM',
            'whatsapp_greeting' => "Hi MJ's Bikes, I'm inquiring about your showroom and repair services.",
            'site_logo' => null,
            'site_favicon' => null,
            'logo_url' => null,
            'favicon_url' => null,
            'footer_email' => 'info@mjsbikes.com',
            'footer_phone' => '+254 712 345 678',
            'footer_address' => 'Mombasa Road, Opp. Cabanas, Nairobi, Kenya',
            'footer_facebook' => 'https://facebook.com',
            'footer_instagram' => 'https://instagram.com',
            'footer_whatsapp' => '+254 712 345 678',
            'whatsapp_clean' => self::cleanWhatsApp('+254 712 345 678'),
        ];
    }

    protected static function assetIfExists(?string $path): ?string
    {
        if (!$path) {
            return null;
        }

        $normalized = ltrim($path, '/');

        if (file_exists(public_path($normalized))) {
            return asset($normalized);
        }

        if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
            return $path;
        }

        return null;
    }

    public static function cleanWhatsApp(?string $number): string
    {
        $waClean = preg_replace('/[^0-9]/', '', (string) $number);

        if (str_starts_with($waClean, '0')) {
            $waClean = '254' . substr($waClean, 1);
        }

        return $waClean;
    }
}
