<?php

namespace App\Models;

use App\Traits\BlameableTrait;
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 Spatie\Permission\Traits\HasRoles;

class Admin extends Authenticatable
{
    use HasFactory, Notifiable, HasRoles, FilterableTrait, BlameableTrait, LogsActivityTrait;

    protected $guard_name = 'admin';

    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 = 'admin';
    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;
    }

    /**
     * Relationship: Created by Admin
     */
    public function createdBy()
    {
        return $this->belongsTo(Admin::class, 'created_by_id');
    }

    /**
     * Relationship: Updated by Admin
     */
    public function updatedBy()
    {
        return $this->belongsTo(Admin::class, 'updated_by_id');
    }
}
