<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * Disable auto-incrementing UUID / String primary keys support
     */
    protected $keyType = 'string';
    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     * Including custom dynamic profile extensions like designation, phone, bio and pictures
     */
    protected $fillable = [
        'id',
        'company_id',
        'branch_id',
        'name',
        'email',
        'password',
        'role',
        'is_active',
        'photo_url',
        'phone',
        'designation',
        'bio',
    ];

    /**
     * The attributes that should be hidden for serialization.
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get user avatar with fallbacks
     */
    public function getAvatarAttribute()
    {
        return $this->photo_url ?: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=crop&w=150&q=80';
    }

    /**
     * Define association with the multi-tenant Company entity
     */
    public function company()
    {
        return $this->belongsTo(Company::class, 'company_id');
    }

    /**
     * Define association with operating Branch locations
     */
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    /**
     * Check if user is inside a specific role dynamic permission key
     */
    public function hasRole(string $targetRole)
    {
        return $this->role === $targetRole;
    }

    /**
     * Check if user represents Platform level Master Admin
     */
    public function isSuperAdmin()
    {
        return $this->role === 'Super Admin';
    }
}
