<?php
// app/Models/Content.php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Content extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'contents';

    protected $fillable = [
        'title', 'title_en', 'title_ar', 'slug',
        'short_description', 'short_description_en', 'short_description_ar',
        'description', 'description_en', 'description_ar',
        'content', 'content_en', 'content_ar',
        'url', 'meta_title', 'meta_description', 'meta_keywords', 'canonical_url',
        'ayat', 'ayat_en', 'ayat_ar', 'surah_name_en', 'surah_name_ar',
        'ayat_number', 'hadith', 'hadith_en', 'hadith_ar',
        'hadith_reference', 'hadith_reference_en', 'hadith_reference_ar',
        'tafsir_en', 'tafsir_ar', 'reference', 'reference_en', 'reference_ar',
        'scholar_name', 'scholar_name_en', 'scholar_name_ar',
        'featured_image', 'thumbnail_image', 'gallery_images', 'video_url',
        'audio_url', 'pdf_url',
        'category_id', 'author_id',
        'status', 'content_type', 'is_featured', 'is_trending', 'is_breaking',
        'allow_comments', 'view_count', 'like_count', 'share_count',
        'og_title', 'og_description', 'og_image',
        'twitter_title', 'twitter_description', 'twitter_image',
        'published_at', 'expires_at',
        'custom_fields', 'language', 'reading_time'
    ];

    protected $casts = [
        'gallery_images' => 'array',
        'custom_fields' => 'array',
        'is_featured' => 'boolean',
        'is_trending' => 'boolean',
        'is_breaking' => 'boolean',
        'allow_comments' => 'boolean',
        'published_at' => 'datetime',
        'expires_at' => 'datetime'
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($content) {
            if (empty($content->slug)) {
                $content->slug = Str::slug($content->title_en);
            }
            if (empty($content->url)) {
                $content->url = '/' . $content->slug;
            }
        });
    }

    // Helper methods to get localized content
    public function getLocalizedTitle($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->title,
            'en' => $this->title_en,
            'ar' => $this->title_ar,
            default => $this->title_en,
        };
    }

    public function getLocalizedShortDescription($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->short_description,
            'en' => $this->short_description_en,
            'ar' => $this->short_description_ar,
            default => $this->short_description_en,
        };
    }

    public function getLocalizedDescription($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->description,
            'en' => $this->description_en,
            'ar' => $this->description_ar,
            default => $this->description_en,
        };
    }

    public function getLocalizedContent($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->content,
            'en' => $this->content_en,
            'ar' => $this->content_ar,
            default => $this->content_en,
        };
    }

    public function getLocalizedAyat($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->ayat,
            'en' => $this->ayat_en,
            'ar' => $this->ayat_ar,
            default => $this->ayat_en,
        };
    }

    public function getLocalizedHadith($locale = null)
    {
        $locale = $locale ?? app()->getLocale();

        return match($locale) {
            'bn' => $this->hadith,
            'en' => $this->hadith_en,
            'ar' => $this->hadith_ar,
            default => $this->hadith_en,
        };
    }

    // Relationships
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    // public function tags()
    // {
    //     return $this->morphToMany(Tag::class, 'taggable');
    // }

    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    // Scopes
    public function scopePublished($query)
    {
        return $query->where('status', 'published')
                     ->where('published_at', '<=', now());
    }

    public function scopeFeatured($query)
    {
        return $query->where('is_featured', true);
    }

    public function scopeTrending($query)
    {
        return $query->where('is_trending', true);
    }

    public function scopeBreaking($query)
    {
        return $query->where('is_breaking', true);
    }

    public function scopeByContentType($query, $type)
    {
        return $query->where('content_type', $type);
    }

    public function scopeByCategory($query, $categoryId)
    {
        return $query->where('category_id', $categoryId);
    }

    public function scopePopular($query)
    {
        return $query->orderBy('view_count', 'desc');
    }

    // Increment view count
    public function incrementViews()
    {
        $this->increment('view_count');
    }

    // Get reading time in minutes
    public function getReadingTimeInMinutes()
    {
        if ($this->reading_time) {
            return $this->reading_time;
        }

        $wordCount = str_word_count(strip_tags($this->getLocalizedContent()));
        return ceil($wordCount / 200); // Assuming 200 words per minute
    }
}
