<?php

namespace App\Models\Site;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Site sayfası. `home` ve `contact` standarttır ve silinemez;
 * `tours` / `tour_detail` sanal sayfalardır (içerik tur modellerinden gelir,
 * section listesi yalnızca sayfa üstü/altı bloklar içindir).
 */
class AgencySitePage extends Model
{
    public const TYPES = ['home', 'contact', 'tours', 'tour_detail', 'custom'];
    public const PROTECTED_TYPES = ['home', 'contact'];

    protected $fillable = [
        'site_id', 'type', 'slug', 'title', 'meta',
        'show_in_nav', 'nav_label', 'nav_order', 'is_active',
    ];

    protected $casts = [
        'meta' => 'array',
        'show_in_nav' => 'boolean',
        'is_active' => 'boolean',
    ];

    public function site(): BelongsTo
    {
        return $this->belongsTo(AgencySite::class, 'site_id');
    }

    public function sections(): HasMany
    {
        return $this->hasMany(AgencySiteSection::class, 'page_id')->orderBy('sort_order');
    }

    public function activeSections(): HasMany
    {
        return $this->sections()->where('is_active', true);
    }

    public function isProtected(): bool
    {
        return in_array($this->type, self::PROTECTED_TYPES, true);
    }

    public function navLabel(): string
    {
        return $this->nav_label ?: $this->title;
    }
}
