<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $fillable = [
        'user_id',
        'full_name',
        'address',
        'phone',
        'avatar_path',
        'is_active',
        'activated_at',
        'deactivated_at',
        'job_title',
        'attendance_time',
        'departure_time',
        'allow_outside_company',
        'notes',
        'base_salary',
        'role_id',
        'general_percentage',
        'role_percentage',
    ];

    protected $casts = [
        'user_id' => 'int',
        'base_salary' => 'int',
        'general_percentage' => 'int',
        'role_percentage' => 'int',
        'role_id' => 'int',
        'allow_outside_company' => 'bool',
        'is_active' => 'bool',
        'activated_at' => 'datetime',
        'deactivated_at' => 'datetime',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function serviceCommissions()
    {
        return $this->hasMany(EmployeeServiceCommission::class);
    }

    public function commissions()
    {
        return $this->hasMany(EmployeeCommission::class);
    }

    public function penalties()
    {
        return $this->hasMany(EmployeePenalty::class);
    }

    public function bonuses()
    {
        return $this->hasMany(EmployeeBonus::class);
    }

    public function withdrawals()
    {
        return $this->hasMany(EmployeeWithdrawal::class);
    }

    public function getAvatarUrlAttribute(): ?string
    {
        if (! $this->avatar_path) {
            return null;
        }

        return '/storage/'.ltrim($this->avatar_path, '/');
    }
}
