<?php

declare(strict_types=1);

namespace App\Livewire\Admin\Settings;

use App\Contracts\Services\SettingsServiceInterface;
use App\Models\Setting;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.admin', ['title' => 'SEO & search'])]
final class SeoSettings extends Component
{
    public bool $seo_allow_indexing = true;

    public string $seo_default_meta_description = '';

    public string $seo_home_meta_title = '';

    public string $seo_home_meta_description = '';

    public string $seo_trust_directory_meta_title = '';

    public string $seo_trust_directory_meta_description = '';

    public string $seo_og_default_image = '';

    public string $seo_twitter_site = '';

    public function mount(): void
    {
        $s = config('settings', []);

        $this->seo_allow_indexing = (bool) ($s['seo_allow_indexing'] ?? true);
        $this->seo_default_meta_description = (string) ($s['seo_default_meta_description'] ?? '');
        $this->seo_home_meta_title = (string) ($s['seo_home_meta_title'] ?? '');
        $this->seo_home_meta_description = (string) ($s['seo_home_meta_description'] ?? '');
        $this->seo_trust_directory_meta_title = (string) ($s['seo_trust_directory_meta_title'] ?? '');
        $this->seo_trust_directory_meta_description = (string) ($s['seo_trust_directory_meta_description'] ?? '');
        $this->seo_og_default_image = (string) ($s['seo_og_default_image'] ?? '');
        $this->seo_twitter_site = (string) ($s['seo_twitter_site'] ?? '');
    }

    public function save(SettingsServiceInterface $settingsService)
    {
        $this->validate([
            'seo_allow_indexing' => ['boolean'],
            'seo_default_meta_description' => ['nullable', 'string', 'max:500'],
            'seo_home_meta_title' => ['nullable', 'string', 'max:200'],
            'seo_home_meta_description' => ['nullable', 'string', 'max:500'],
            'seo_trust_directory_meta_title' => ['nullable', 'string', 'max:200'],
            'seo_trust_directory_meta_description' => ['nullable', 'string', 'max:500'],
            'seo_og_default_image' => ['nullable', 'string', 'max:2048'],
            'seo_twitter_site' => ['nullable', 'string', 'max:64'],
        ]);

        $definitions = [
            'seo_allow_indexing' => [
                'value' => $this->seo_allow_indexing ? '1' : '0',
                'type' => 'boolean',
                'description' => 'Allow search engines to index the public site (disable on staging).',
            ],
            'seo_default_meta_description' => [
                'value' => $this->seo_default_meta_description,
                'type' => 'string',
                'description' => 'Fallback meta description when a page does not define one.',
            ],
            'seo_home_meta_title' => [
                'value' => $this->seo_home_meta_title,
                'type' => 'string',
                'description' => 'Full homepage <title> line (leave blank for default “Blog | site name”).',
            ],
            'seo_home_meta_description' => [
                'value' => $this->seo_home_meta_description,
                'type' => 'string',
                'description' => 'Homepage meta description for Google snippets.',
            ],
            'seo_trust_directory_meta_title' => [
                'value' => $this->seo_trust_directory_meta_title,
                'type' => 'string',
                'description' => 'Trust checks directory page title (leave blank for default).',
            ],
            'seo_trust_directory_meta_description' => [
                'value' => $this->seo_trust_directory_meta_description,
                'type' => 'string',
                'description' => 'Trust checks directory meta description.',
            ],
            'seo_og_default_image' => [
                'value' => $this->seo_og_default_image,
                'type' => 'string',
                'description' => 'Default Open Graph / social share image URL or storage path.',
            ],
            'seo_twitter_site' => [
                'value' => $this->seo_twitter_site,
                'type' => 'string',
                'description' => 'Twitter / X handle for twitter:site (e.g. @yourbrand).',
            ],
        ];

        foreach ($definitions as $key => $meta) {
            Setting::query()->updateOrCreate(
                ['key' => $key],
                [
                    'value' => $meta['value'],
                    'type' => $meta['type'],
                    'group' => 'seo',
                    'description' => $meta['description'],
                ]
            );
        }

        $settingsService->refresh();

        session()->flash('success', 'SEO settings saved.');

        return $this->redirect(route('admin.settings.seo'), navigate: true);
    }

    public function render()
    {
        return view('livewire.admin.settings.seo-settings');
    }
}
