<?php
declare(strict_types=1);

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Teacher extends Entity
{
    protected array $_accessible = [
        'slug' => true,
        'full_name' => true,
        'role' => true,
        'bio' => true,
        'specialties' => true,
        'photo_path' => true,
        'hourly_rate_cents' => true,
        'is_active' => true,
        'sort_order' => true,
        'created' => true,
        'modified' => true,
    ];

    /**
     * @return list<string>
     */
    public function getSpecialtyList(): array
    {
        $raw = trim((string)($this->specialties ?? ''));
        if ($raw === '') {
            return [];
        }
        return array_values(array_filter(array_map('trim', explode(',', $raw))));
    }

    public function getInitials(): string
    {
        $parts = preg_split('/\s+/', trim((string)$this->full_name)) ?: [];
        $letters = '';
        foreach ($parts as $p) {
            if ($p !== '') {
                $letters .= mb_strtoupper(mb_substr($p, 0, 1));
                if (mb_strlen($letters) >= 2) break;
            }
        }
        return $letters;
    }

    public function getHourlyDollars(): string
    {
        $cents = (int)($this->hourly_rate_cents ?? 0);
        return number_format($cents / 100, 0);
    }
}
