<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
 * Thin local mirror of the platform identity (auth.ladill.com owns users).
 * Keyed by the OIDC `sub` (public_id); every Care record is scoped to it.
 */
class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    protected $fillable = ['public_id', 'name', 'email', 'avatar_url', 'password', 'last_app_active_at'];

    protected $hidden = ['password', 'remember_token'];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'last_app_active_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function ownerRef(): string
    {
        return (string) $this->public_id;
    }

    public function avatarUrl(): ?string
    {
        $url = trim((string) $this->avatar_url);

        return $url !== '' ? $url : null;
    }
}
