<?php

namespace FluxCms\Core\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Translatable\HasTranslations;

class NavigationItem extends Model
{
    use HasTranslations;

    public function getTable()
    {
        return config('flux-cms.database.table_prefix', 'flux_cms_').'navigation_items';
    }

    protected $fillable = [
        'navigation_id',
        'parent_id',
        'page_id',
        'title',
        'url',
        'target',
        'order',
        'is_active',
        'settings',
    ];

    protected $translatable = [
        'title',
    ];

    protected $casts = [
        'title' => 'array',
        'settings' => 'array',
        'is_active' => 'boolean',
        'order' => 'integer',
    ];

    public function navigation(): BelongsTo
    {
        return $this->belongsTo(Navigation::class, 'navigation_id');
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(NavigationItem::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'parent_id')
            ->where('is_active', true)
            ->orderBy('order');
    }

    public function allChildren(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'parent_id')
            ->orderBy('order');
    }

    public function page(): BelongsTo
    {
        return $this->belongsTo(Page::class, 'page_id');
    }

    /**
     * Scopes
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeTopLevel($query)
    {
        return $query->whereNull('parent_id');
    }

    public function scopeForNavigation($query, int $navigationId)
    {
        return $query->where('navigation_id', $navigationId);
    }

    /**
     * Get the effective URL for this navigation item
     */
    public function getEffectiveUrl(): string
    {
        if ($this->page_id && $this->page) {
            return $this->page->getUrl();
        }

        return $this->url ?? '#';
    }

    /**
     * Check if this item has children
     */
    public function hasChildren(): bool
    {
        return $this->children()->count() > 0;
    }

    /**
     * Get all descendant IDs (children, grandchildren, etc.)
     */
    public function getDescendantIds(): array
    {
        $ids = [];
        $this->collectDescendantIds($ids);

        return $ids;
    }

    protected function collectDescendantIds(array &$ids): void
    {
        foreach ($this->allChildren as $child) {
            $ids[] = $child->id;
            $child->collectDescendantIds($ids);
        }
    }

    /**
     * Settings management
     */
    public function getSetting(string $key, mixed $default = null): mixed
    {
        return data_get($this->settings, $key, $default);
    }

    public function setSetting(string $key, mixed $value): void
    {
        $settings = $this->settings ?? [];
        data_set($settings, $key, $value);
        $this->update(['settings' => $settings]);
    }
}
