<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Pathway extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'logo',
        'short_description',
        'status',
        'sorting'
    ];

    protected $casts = [
        'status' => 'boolean',
        'sorting' => 'integer'
    ];

    /**
     * Scope to get only active pathways
     */
    public function scopeActive($query)
    {
        return $query->where('status', true);
    }

    /**
     * Scope to get pathways ordered by sorting
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('sorting', 'asc')->orderBy('name', 'asc');
    }

    /**
     * Scope to search pathways by name or description
     */
    public function scopeSearch($query, $search)
    {
        return $query->where('name', 'like', '%' . $search . '%')
                    ->orWhere('short_description', 'like', '%' . $search . '%');
    }

    /**
     * Get the logo URL
     */
    public function getLogoUrlAttribute()
    {
        if ($this->logo && file_exists(public_path('storage/' . $this->logo))) {
            return asset('storage/' . $this->logo);
        }
        return asset('img/default-pathway.png'); // Default image
    }

    /**
     * Get status as text
     */
    public function getStatusTextAttribute()
    {
        return $this->status ? 'Active' : 'Inactive';
    }
}