<?php

namespace App\Models;

use Database\Factories\MenuFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * @property int $id
 * @property string $name
 * @property string $slug
 * @property string|null $icon
 * @property int|null $parent_id
 * @property int $display_order
 * @property bool $is_visible
 * @property bool $requires_auth
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read Menu|null $parent
 * @property-read Collection<int, Menu> $children
 * @property-read int|null $children_count
 * @property-read Collection<int, Page> $pages
 * @property-read int|null $pages_count
 *
 * @method static \Database\Factories\MenuFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Menu newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Menu newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Menu query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Menu visible()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|Menu parentOnly()
 *
 * @property-read Collection<int, Page> $submenuPages
 * @property-read int|null $submenu_pages_count
 *
 * @method static Builder<static>|Menu whereCreatedAt($value)
 * @method static Builder<static>|Menu whereDisplayOrder($value)
 * @method static Builder<static>|Menu whereIcon($value)
 * @method static Builder<static>|Menu whereId($value)
 * @method static Builder<static>|Menu whereIsVisible($value)
 * @method static Builder<static>|Menu whereName($value)
 * @method static Builder<static>|Menu whereParentId($value)
 * @method static Builder<static>|Menu whereRequiresAuth($value)
 * @method static Builder<static>|Menu whereSlug($value)
 * @method static Builder<static>|Menu whereUpdatedAt($value)
 *
 * @mixin \Eloquent
 */
class Menu extends Model
{
    /** @use HasFactory<MenuFactory> */
    use HasFactory;

    protected $fillable = [
        'name',
        'slug',
        'icon',
        'parent_id',
        'display_order',
        'is_visible',
        'requires_auth',
    ];

    protected function casts(): array
    {
        return [
            'is_visible' => 'boolean',
            'requires_auth' => 'boolean',
        ];
    }

    /**
     * @return BelongsTo<Menu, $this>
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Menu::class, 'parent_id');
    }

    /**
     * @return HasMany<Menu, $this>
     */
    public function children(): HasMany
    {
        return $this->hasMany(Menu::class, 'parent_id')
            ->orderBy('display_order');
    }

    /**
     * @return HasMany<Page, $this>
     */
    public function pages(): HasMany
    {
        return $this->hasMany(Page::class, 'menu_id')
            ->orderBy('display_order');
    }

    /**
     * @return HasMany<Page, $this>
     */
    public function submenuPages(): HasMany
    {
        return $this->hasMany(Page::class, 'submenu_id')
            ->orderBy('display_order');
    }

    /**
     * @param  Builder<static>  $query
     * @return Builder<static>
     */
    public function scopeVisible(Builder $query): Builder
    {
        return $query->where('is_visible', true);
    }

    /**
     * @param  Builder<static>  $query
     * @return Builder<static>
     */
    public function scopeParentOnly(Builder $query): Builder
    {
        return $query->whereNull('parent_id');
    }

    public static function clearMenuCache(): void
    {
        Cache::forget('dynamic_menus_v2_auth');
        Cache::forget('dynamic_menus_v2_guest');
    }

    protected static function booted(): void
    {
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderBy('display_order');
        });

        static::saved(fn () => static::clearMenuCache());
        static::deleted(fn () => static::clearMenuCache());
    }
}
