<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Guide extends Model
{
    protected $guarded = [];

    protected $casts = [
        'pages' => 'array',
        'is_published' => 'boolean',
    ];

    public function scopePublished(Builder $query): Builder
    {
        return $query->where('is_published', true);
    }

    public function coverUrl(): string
    {
        if (! $this->cover_path) {
            return asset('images/photo-bulb.jpg');
        }

        return Str::startsWith($this->cover_path, 'images/')
            ? asset($this->cover_path)
            : asset('storage/'.$this->cover_path);
    }

    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}
