<?php

namespace App\Livewire\Website;

use App\Domain\Website\Services\WebsiteSettingService;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('تصميم الموقع')]
class ThemeEditor extends Component
{
    public string $primary_color = '#1a1a2e';
    public string $secondary_color = '#16213e';
    public string $accent_color = '#e94560';
    public string $text_color = '#ffffff';
    public string $heading_font = 'Cairo';
    public string $body_font = 'Cairo';
    public string $navbar_style = 'transparent';
    public string $site_title = '';
    public string $site_title_en = '';
    public string $site_description = '';
    public string $site_description_en = '';
    public string $whatsapp_number = '';
    public array $social_links = [];
    public string $custom_css = '';
    public string $google_analytics_id = '';
    public string $facebook_pixel_id = '';

    public array $availableFonts = [
        'Cairo', 'Tajawal', 'IBM Plex Sans Arabic', 'Noto Sans Arabic',
        'Almarai', 'Readex Pro', 'Rubik', 'El Messiri',
    ];

    public function mount(): void
    {
        $this->authorize('settings.manage');

        $settings = app(WebsiteSettingService::class)->getOrCreate(app('current_academy'));

        $this->primary_color = $settings->primary_color;
        $this->secondary_color = $settings->secondary_color;
        $this->accent_color = $settings->accent_color;
        $this->text_color = $settings->text_color;
        $this->heading_font = $settings->heading_font;
        $this->body_font = $settings->body_font;
        $this->navbar_style = $settings->navbar_style;
        $this->site_title = $settings->site_title ?? '';
        $this->site_title_en = $settings->site_title_en ?? '';
        $this->site_description = $settings->site_description ?? '';
        $this->site_description_en = $settings->site_description_en ?? '';
        $this->whatsapp_number = $settings->whatsapp_number ?? '';
        $this->social_links = $settings->social_links ?? [];
        $this->custom_css = $settings->custom_css ?? '';
        $this->google_analytics_id = $settings->google_analytics_id ?? '';
        $this->facebook_pixel_id = $settings->facebook_pixel_id ?? '';
    }

    public function save(): void
    {
        $this->validate([
            'primary_color' => 'required|regex:/^#[0-9a-fA-F]{6}$/',
            'secondary_color' => 'required|regex:/^#[0-9a-fA-F]{6}$/',
            'accent_color' => 'required|regex:/^#[0-9a-fA-F]{6}$/',
            'text_color' => 'required|regex:/^#[0-9a-fA-F]{6}$/',
            'heading_font' => 'required|string|max:50',
            'body_font' => 'required|string|max:50',
            'navbar_style' => 'required|in:solid,transparent,floating',
            'site_title' => 'nullable|string|max:255',
            'site_description' => 'nullable|string|max:1000',
            'whatsapp_number' => 'nullable|string|max:20',
        ]);

        app(WebsiteSettingService::class)->update(app('current_academy'), [
            'primary_color' => $this->primary_color,
            'secondary_color' => $this->secondary_color,
            'accent_color' => $this->accent_color,
            'text_color' => $this->text_color,
            'heading_font' => $this->heading_font,
            'body_font' => $this->body_font,
            'navbar_style' => $this->navbar_style,
            'site_title' => $this->site_title,
            'site_title_en' => $this->site_title_en,
            'site_description' => $this->site_description,
            'site_description_en' => $this->site_description_en,
            'whatsapp_number' => $this->whatsapp_number,
            'social_links' => $this->social_links,
            'custom_css' => $this->custom_css,
            'google_analytics_id' => $this->google_analytics_id,
            'facebook_pixel_id' => $this->facebook_pixel_id,
        ]);

        session()->flash('success', __('تم حفظ إعدادات التصميم بنجاح'));
    }

    public function render()
    {
        return view('livewire.website.theme-editor');
    }
}
