<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class HomepageLayout extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'label',
        'description',
        'is_active',
        'sort_order',
        'hero_layout',
        'hero_content_position',
        'show_hero_badge',
        'show_hero_subtitle',
        'show_hero_buttons',
        'hero_title',
        'hero_subtitle',
        'hero_description',
        'hero_badge_text',
        'hero_primary_btn_text',
        'hero_primary_btn_url',
        'hero_secondary_btn_text',
        'hero_secondary_btn_url',
        'hero_min_height',
        'hero_overlay_opacity',
        'hero_bg_image_url',
        'hero_animated',
        'hero_show_scroll_arrow',
        'stats_layout',
        'show_stats_section',
        'stats_columns',
        'stats_section_title',
        'stats_show_icons',
        'stats_style',
        'show_blog_section',
        'blog_columns',
        'blog_limit',
        'blog_section_title',
        'blog_section_subtitle',
        'blog_card_style',
        'blog_show_author',
        'blog_show_date',
        'blog_show_views',
        'blog_show_readmore',
        'blog_view_all_text',
        'show_features_section',
        'features_columns',
        'features_section_title',
        'features_section_subtitle',
        'features_style',
        'show_features_for_guests_only',
        'show_forum_section',
        'forum_topics_limit',
        'forum_section_title',
        'show_library_section',
        'library_limit',
        'library_section_title',
        'show_cta_section',
        'cta_position',
        'cta_title',
        'cta_description',
        'cta_primary_btn_text',
        'cta_primary_btn_url',
        'cta_secondary_btn_text',
        'cta_secondary_btn_url',
        'cta_style',
        'section_order',
        'max_container_width',
        'section_rounded',
        'section_shadows',
        'dark_mode_support',
        'meta_title',
        'meta_description',
        'use_widget_system',
        'widget_order',
        'custom_css',
        'custom_html_before_hero',
        'custom_html_after_hero',
        'custom_html_after_stats',
        'custom_html_before_cta',
        'custom_html_after_cta',
        'preview_image',
        'settings',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'show_hero_badge' => 'boolean',
        'show_hero_subtitle' => 'boolean',
        'show_hero_buttons' => 'boolean',
        'hero_animated' => 'boolean',
        'hero_show_scroll_arrow' => 'boolean',
        'stats_show_icons' => 'boolean',
        'show_stats_section' => 'boolean',
        'show_blog_section' => 'boolean',
        'blog_show_author' => 'boolean',
        'blog_show_date' => 'boolean',
        'blog_show_views' => 'boolean',
        'blog_show_readmore' => 'boolean',
        'show_features_section' => 'boolean',
        'show_features_for_guests_only' => 'boolean',
        'show_forum_section' => 'boolean',
        'show_library_section' => 'boolean',
        'show_cta_section' => 'boolean',
        'section_rounded' => 'boolean',
        'section_shadows' => 'boolean',
        'dark_mode_support' => 'boolean',
        'use_widget_system' => 'boolean',
        'settings' => 'array',
    ];

    /**
     * Get the active homepage layout
     */
    public static function getActiveLayout(): ?self
    {
        return Cache::remember('homepage_layout_active', 3600, function () {
            return self::where('is_active', true)->first();
        });
    }

    /**
     * Get all layouts ordered by sort_order
     */
    public static function getAllLayouts(): \Illuminate\Database\Eloquent\Collection
    {
        return Cache::remember('homepage_layouts_all', 3600, function () {
            return self::orderBy('sort_order')->get();
        });
    }

    /**
     * Get layout by name
     */
    public static function getByName(string $name): ?self
    {
        return self::where('name', $name)->first();
    }

    /**
     * Activate a layout (deactivate all others)
     */
    public function activate(): void
    {
        self::query()->update(['is_active' => false]);
        $this->is_active = true;
        $this->save();
        self::clearCache();
    }

    /**
     * Clear layout cache
     */
    public static function clearCache(): void
    {
        Cache::forget('homepage_layout_active');
        Cache::forget('homepage_layouts_all');
    }

    /**
     * Boot method to clear cache on model changes
     */
    protected static function boot()
    {
        parent::boot();

        static::saved(function () {
            self::clearCache();
        });

        static::deleted(function () {
            self::clearCache();
        });
    }

    /**
     * Get hero layout class
     */
    public function getHeroLayoutClass(): string
    {
        return match($this->hero_layout) {
            'split' => 'hero-split',
            'full-width' => 'hero-full-width',
            'video-bg' => 'hero-video-bg',
            default => 'hero-centered',
        };
    }

    /**
     * Get stats layout class
     */
    public function getStatsLayoutClass(): string
    {
        return match($this->stats_layout) {
            'grid-2' => 'stats-grid-2',
            'carousel' => 'stats-carousel',
            'none' => 'stats-none',
            default => 'stats-grid-4',
        };
    }
}
