<?php

namespace Database\Seeders;

use App\Models\Media;
use App\Models\Setting;
use Illuminate\Database\Seeder;

class SettingSeeder extends Seeder
{
    public function run(): void
    {
        Setting::updateOrCreate(
            ['id' => 1],
            [
                'site_name' => 'Saleem Sial Analytics',
                'tagline' => 'GA4, GTM and performance tracking specialist',
                'logo' => 'uploads/logos/saleem-sial-logo.png',
                'logo_media_id' => $this->preferredMediaId([
                    'uploads/system/saleem-sial-logo.png',
                    'uploads/system/saleem-sial-logo.jpeg',
                ]),
                'favicon' => 'uploads/placeholders/saleem-sial-favicon.png',
                'favicon_media_id' => $this->preferredMediaId([
                    'uploads/system/saleem-sial-favicon.png',
                    'uploads/system/default-favicon.ico',
                ]),
                'contact_email' => 'hello@saleemsial.com',
                'contact_phone' => '+92 300 1234567',
                'footer_text' => 'Saleem Sial Analytics builds clean, scalable tracking systems for advertisers, agencies, and growth teams.',
                'social_links' => [
                    'facebook' => 'https://facebook.com/#',
                    'instagram' => 'https://instagram.com/#',
                    'x' => 'https://x.com/#',
                    'linkedin' => 'https://linkedin.com/#',
                ],
                'header_cta_label' => 'Book a Consultation',
                'header_cta_url' => '/contact',
                'footer_cta_label' => 'Start Your Tracking Setup',
                'footer_cta_url' => '/contact',
                'admin_contact_notifications_enabled' => true,
                'user_contact_auto_reply_enabled' => true,
                'support_chat_enabled' => true,
                'support_chat_title' => 'Need quick support?',
                'support_chat_intro' => 'Send a direct message about your tracking issue and we will follow up by email.',
                'support_chat_button_label' => 'Send support message',
                'admin_contact_subject' => 'New website inquiry from {{ name }}',
                'admin_contact_template' => <<<HTML
<p>You have received a new inquiry on {{ site_name }}.</p>
<p><strong>Name:</strong> {{ name }}</p>
<p><strong>Email:</strong> {{ email }}</p>
<p><strong>Phone:</strong> {{ phone }}</p>
<p><strong>Source:</strong> {{ source_label }}</p>
<p><strong>Page URL:</strong> {{ page_url }}</p>
<p><strong>Submitted:</strong> {{ submitted_at }}</p>
<p><strong>Message:</strong></p>
<p>{{ message }}</p>
HTML,
                'user_contact_subject' => 'Thanks for contacting {{ site_name }}',
                'user_contact_template' => <<<HTML
<p>Hello {{ name }},</p>
<p>Thanks for reaching out to {{ site_name }}. Your message has been received and we will respond as soon as possible.</p>
<p><strong>Source:</strong> {{ source_label }}</p>
<p><strong>Your message:</strong></p>
<p>{{ message }}</p>
<p>You can reply directly to {{ contact_email }} if you need to share more details.</p>
HTML,
                'gtm_container_id' => null,
                'ga_measurement_id' => null,
            ]
        );
    }

    protected function mediaId(string $path): ?int
    {
        return Media::query()->where('file_path', $path)->value('id');
    }

    protected function preferredMediaId(array $paths): ?int
    {
        foreach ($paths as $path) {
            $id = $this->mediaId($path);

            if ($id) {
                return $id;
            }
        }

        return null;
    }
}
