<?php

namespace App\Models;

use App\Enums\EmploymentStatus;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

#[Appends(['formatted_base_salary', 'employment_status_label', 'formatted_join_date', 'formatted_resign_date'])]
#[Guarded(['id'])]
class Employee extends Model
{
    use HasFactory, SoftDeletes;

    protected function casts(): array
    {
        return [
            'employment_status' => EmploymentStatus::class,
            'base_salary' => 'integer',
            'join_date' => 'date:Y-m-d',
            'resign_date' => 'date:Y-m-d',
        ];
    }

    protected function formattedBaseSalary(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->base_salary, 0, ',', '.'),
        );
    }

    protected function employmentStatusLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->employment_status->label(),
        );
    }

    protected function formattedJoinDate(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->join_date?->translatedFormat('l, d F Y'),
        );
    }

    protected function formattedResignDate(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->resign_date?->translatedFormat('l, d F Y'),
        );
    }

    #[Scope]
    protected function contract(Builder $query): void
    {
        $query->where('employment_status', EmploymentStatus::CONTRACT);
    }

    #[Scope]
    protected function fullTime(Builder $query): void
    {
        $query->where('employment_status', EmploymentStatus::FULL_TIME);
    }

    #[Scope]
    protected function internship(Builder $query): void
    {
        $query->where('employment_status', EmploymentStatus::INTERNSHIP);
    }

    #[Scope]
    protected function partTime(Builder $query): void
    {
        $query->where('employment_status', EmploymentStatus::PART_TIME);
    }

    #[Scope]
    protected function resigned(Builder $query): void
    {
        $query->where('employment_status', EmploymentStatus::RESIGNED);
    }

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

    public function employeeAdvances(): HasMany
    {
        return $this->hasMany(EmployeeAdvance::class);
    }

    public function leaveRequests(): HasMany
    {
        return $this->hasMany(LeaveRequest::class);
    }

    public function payrolls(): HasMany
    {
        return $this->hasMany(Payroll::class);
    }

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