<?php

namespace App\Services;

use App\Enums\ContentStatus;
use App\Models\Client;
use App\Models\GalleryItem;
use App\Models\NavigationItem;
use App\Models\PageContent;
use App\Models\Partner;
use App\Models\Service;
use Illuminate\Support\Facades\Schema;

class SiteContentService
{
    public function __construct(
        private readonly SettingsService $settings,
        private readonly ContentCacheService $cache,
    ) {}

    public function page(string $slug): array
    {
        if (! Schema::hasTable('page_contents')) {
            return $this->fallbackPage($slug);
        }

        return $this->cache->remember("page:$slug", function () use ($slug): array {
            $page = PageContent::where('slug', $slug)->first();

            if (! $page) {
                return $this->fallbackPage($slug);
            }

            return [
                'content' => $page->publicContent(),
                'meta' => $page->publicSeo() ?: $this->fallbackMeta($slug),
                'model' => $page,
            ];
        });
    }

    public function company(): array
    {
        return $this->settings->group('general', config('nh_trans.company', []));
    }

    public function contact(): array
    {
        return $this->settings->group('contact', config('nh_trans.contact', ['email' => null, 'phones' => []]));
    }

    public function footer(): array
    {
        return $this->settings->group('footer', config('nh_trans.footer', []));
    }

    public function email(): array
    {
        return $this->settings->group('email', [
            'sender_name' => 'NH TRANS',
            'sender_address' => null,
            'reply_to' => null,
            'notification_address' => null,
            'cc_address' => null,
            'signature' => "L'équipe NH TRANS",
        ]);
    }

    public function seoDefaults(): array
    {
        return $this->settings->group('seo', [
            'site_name' => 'NH TRANS',
            'default_title' => config('nh_trans.meta.home.title'),
            'default_description' => config('nh_trans.meta.home.description'),
            'indexable' => true,
            'robots' => 'index, follow',
            'og_locale' => 'fr_FR',
        ]);
    }

    public function navigation(string $location = 'header'): array
    {
        if (! Schema::hasTable('navigation_items')) {
            return config('nh_trans.nav', []);
        }

        return NavigationItem::query()
            ->active()
            ->where('location', $location)
            ->whereNull('parent_id')
            ->ordered()
            ->get()
            ->map(fn (NavigationItem $item): array => [
                'id' => $item->id,
                'label' => $item->label,
                'route' => $item->route_name,
                'route_name' => $item->route_name,
                'url' => $item->url,
                'target' => $item->target,
                'sort_order' => $item->sort_order,
                'is_active' => $item->is_active,
            ])
            ->all();
    }

    public function services(bool $publicOnly = true)
    {
        if (! Schema::hasTable('services')) {
            return collect(config('nh_trans.services', []));
        }

        $query = Service::query()->with(['media', 'highlights' => fn ($q) => $q->active()->orderBy('sort_order')])->ordered();

        if ($publicOnly) {
            $query->published()->active();
        }

        return $query->get();
    }

    public function partners(bool $publicOnly = true)
    {
        if (! Schema::hasTable('partners')) {
            return collect(config('nh_trans.partners', []));
        }

        $query = Partner::query()->with('media')->ordered();

        if ($publicOnly) {
            $query->published()->active();
        }

        return $query->get();
    }

    public function clients(bool $publicOnly = true)
    {
        if (! Schema::hasTable('clients')) {
            return collect(config('nh_trans.clients', []));
        }

        $query = Client::query()->with('media')->ordered();

        if ($publicOnly) {
            $query->published()->active();
        }

        return $query->get();
    }

    public function gallery(bool $publicOnly = true)
    {
        if (! Schema::hasTable('gallery_items')) {
            return collect(config('nh_trans.gallery', []));
        }

        $query = GalleryItem::query()->with('media')->ordered();

        if ($publicOnly) {
            $query->published()->active();
        }

        return $query->get();
    }

    private function fallbackPage(string $slug): array
    {
        return [
            'content' => [],
            'meta' => $this->fallbackMeta($slug),
            'model' => null,
        ];
    }

    private function fallbackMeta(string $slug): array
    {
        $key = match ($slug) {
            'services-page' => 'services',
            'gallery-page' => 'gallery',
            'contact-page' => 'contact',
            default => $slug,
        };

        return config("nh_trans.meta.$key", config('nh_trans.meta.home'));
    }
}
