<?php

namespace App\Models;

use App\Events\SitemapDataChanged;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'id',
        'type',
        'name',
        'slug',
        'description',
        'parent_id',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'type'      => 'integer',
        'parent_id' => 'integer',
    ];

    protected $appends = [
        'articles_count',
        'full_slug',
    ];

    // Self-referencing relationship for parent category
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'parent_id')->with('parent');
    }

    // Self-referencing relationship for child categories
    public function children(): HasMany
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    // Get all children recursively
    public function allChildren(): HasMany
    {
        return $this->children()->with('allChildren');
    }

    // Many-to-many relationship with articles
    public function articles(): BelongsToMany
    {
        return $this->belongsToMany(Article::class, 'article_categories')
            ->withTimestamps()
            ->wherePivotNull('deleted_at');
    }

    // Many-to-many relationship with articles
    public function publishedArticles(): BelongsToMany
    {
        return $this->belongsToMany(Article::class, 'article_categories')
            ->withTimestamps()
            ->wherePivotNull('deleted_at')
            ->where('status', 'publish');
    }

    // Scope to get root categories (parent_id = 0)
    public function scopeRoots($query)
    {
        return $query->where('parent_id', 0);
    }

    // Get the full path of the category (including parent names)
    public function getFullPathAttribute(): string
    {
        $path = [];
        $category = $this;

        while ($category) {
            array_unshift($path, $category->name);
            $category = $category->parent;
        }

        return implode(' > ', $path);
    }

    // Get articles count (excluding soft deleted)
    public function getArticlesCountAttribute(): int
    {
        return $this->articles()->count();
    }

    // Check if category can be deleted (no articles associated)
    public function canDelete(): bool
    {
        return $this->articles()->count() === 0 && $this->children()->count() === 0;
    }

    // Override delete to handle soft delete of related data
    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($category) {
            if (! $category->canDelete()) {
                throw new \Exception('Cannot delete category that has articles or child categories');
            }
        });

        // Fire sitemap data changed event when category is created, updated, or deleted
        static::created(function ($category) {
            event(new SitemapDataChanged);
        });

        static::updated(function ($category) {
            event(new SitemapDataChanged);
        });

        static::deleted(function ($category) {
            event(new SitemapDataChanged);
        });

        static::restored(function ($category) {
            event(new SitemapDataChanged);
        });
    }

    // Accessor to get full Slug
    public function getFullSlugAttribute()
    {
        $slugs = [];
        $category = $this;

        while ($category) {
            $slugs[] = $category->slug;
            $category = $category->parent;
        }

        return implode('/', array_reverse($slugs));
    }
}
