<?php

namespace App\Models;

use App\Traits\FilterableTrait;
use App\Traits\LogsActivityTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasApiTokens, FilterableTrait, LogsActivityTrait;

    protected $fillable = [
        'name',
        'email',
        'password',
        'phone',
        'image',
        'is_active',
    ];

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

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

    // Filterable configuration
    protected array $filterable = ['name', 'email', 'is_active'];
    protected array $searchable = ['name', 'email', 'phone'];
    protected array $sortable = ['id', 'name', 'email', 'created_at'];

    // Activity Log configuration
    public string $logName = 'user';
    public array $loggable = ['name', 'email', 'phone', 'is_active'];
    public array $maskedInLog = ['password'];

    /**
     * Get the image URL attribute
     */
    public function getImageUrlAttribute(): ?string
    {
        return $this->image ? viewImage($this->image) : null;
    }

    /**
     * Scope: Only active users
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }
}
