<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Sanctum\HasApiTokens;
use LaravelFCM\Message\PayloadNotification;
use FCM;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory;

    protected $fillable = [
        'name', 'email', 'phone', 'password', 'bilhete', 'latitude', 'longitude', 'photo','tipo_conta',
        'tipo_carga','marca_carro','cor_carro','ano_fabrico','matricula','ivm_image_path','assento','carta_conducao','data_emissao',
        'typeUser', 'firebase_token','status'
    ];

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

    protected $casts = [
        'latitude' => 'decimal:8',
        'longitude' => 'decimal:8',
        'typeUser' => 'string',
        
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function aderentes()
    {
        return $this->hasMany(Aderente::class, 'cliente_id');
    }

    // 👇 adiciona isso para expor o accessor
    protected $appends = ['avatar_url'];

    public function getAvatarUrlAttribute()
{
    if (!$this->photo) {
        return null;
    }

    $timestamp = $this->updated_at ? $this->updated_at->timestamp : time();

    return url('storage/' . $this->photo) . '?t=' . $timestamp;
}




public function postsAderidos()
{
    return $this->belongsToMany(Post::class, 'aderentes', 'cliente_id', 'post_id');
}

}
