<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;

    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    const STATUSES = [
        self::STATUS_ACTIVE => 'Aktif',
        self::STATUS_INACTIVE => 'Tidak Aktif',
    ];

    const GENDER_MALE = 'L';
    const GENDER_FEMALE = 'P';

    const GENDERS = [
        self::GENDER_MALE => 'Laki-laki',
        self::GENDER_FEMALE => 'Perempuan',
    ];

    protected $fillable = [
        'user_id',
        'employee_id',
        'name',
        'gender',
        'phone',
        'email',
        'address',
        'position',
        'join_date',
        'base_salary',
        'status',
        'is_auto_attendance',
        'auto_attendance_note',
    ];

    protected $casts = [
        'join_date' => 'date',
        'base_salary' => 'decimal:2',
        'is_auto_attendance' => 'boolean',
    ];

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

    public function salaries()
    {
        return $this->hasMany(Salary::class);
    }

    public function attendances()
    {
        return $this->hasMany(Attendance::class);
    }

    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? 'Unknown';
    }

    public function getGenderLabelAttribute(): string
    {
        return self::GENDERS[$this->gender] ?? 'Unknown';
    }

    public function getFormattedBaseSalaryAttribute(): string
    {
        return 'Rp ' . number_format($this->base_salary, 0, ',', '.');
    }

    public function getAutoAttendanceLabelAttribute(): string
    {
        return $this->is_auto_attendance ? 'Auto Absen' : 'Manual';
    }
}