<?php

namespace App\Models;

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

class Coach extends Model
{
    protected $fillable = [
        'name', 'slug', 'role', 'bio', 'photo_path', 'media_tag', 'tags',
        'show_on_home', 'show_on_about', 'sort_order', 'is_active',
    ];

    protected function casts(): array
    {
        return [
            'tags' => 'array',
            'show_on_home' => 'boolean',
            'show_on_about' => 'boolean',
            'is_active' => 'boolean',
        ];
    }

    public function sessions(): HasMany
    {
        return $this->hasMany(ClassSession::class);
    }

    public function shortName(): string
    {
        $parts = explode(' ', $this->name);

        return ($parts[0] ?? '').' '.(isset($parts[1]) ? substr($parts[1], 0, 1).'.' : '');
    }

    public function photoUrl(): ?string
    {
        if (! $this->photo_path) {
            return null;
        }

        if (str_starts_with($this->photo_path, 'coaches/')) {
            return asset('storage/'.$this->photo_path);
        }

        return asset('images/'.ltrim($this->photo_path, '/'));
    }
}
