<?php

namespace App\Livewire\Studio\Master\User;

use App\Models\Employee;
use App\Models\User;
use App\Traits\WithConfirmation;
use App\Traits\WithToast;
use Flux\Flux;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Pegawai')]
class Index extends Component
{
    use WithConfirmation, WithToast;

    public array $employees = [];

    public string $search = '';

    public array $status = [];

    public function mount()
    {
        $this->loadEmployees();
    }

    protected function loadEmployees()
    {
        $this->employees = Employee::with(['user', 'user.referralCode', 'user.outlets'])
            ->when(! empty($this->search), function ($query) {
                return $query->where('full_name', 'like', '%'.$this->search.'%')
                    ->orWhere('code', 'like', '%'.$this->search.'%')
                    ->orWhereHas('user', fn ($q) => $q->where('email', 'like', '%'.$this->search.'%'))
                    ->orWhereHas('user', fn ($q) => $q->where('username', 'like', '%'.$this->search.'%'));
            })
            ->when(! empty($this->status), fn ($query) => $query->whereHas('user', fn ($q) => $q->whereIn('status', $this->status)))
            ->latest()
            ->get()
            ->map(fn ($employee) => [
                'full_name' => $employee->full_name,
                'code' => $employee->code,
                'phone_number' => $employee->phone_number,
                'address' => $employee->address,
                'base_salary' => currency($employee->base_salary, 'Rp'),
                'gender' => [
                    'label' => $employee->gender->label(),
                    'color' => $employee->gender->color(),
                ],
                'birthdate' => formatDate($employee->birthdate),
                'birth_age' => getAge($employee->birthdate),
                'user' => [
                    'id' => $employee->user?->id,
                    'hash' => $employee->user?->hash,
                    'email' => $employee->user?->email,
                    'username' => $employee->user?->username,
                    'status' => [
                        'gradient' => $employee->user?->status->gradient(),
                    ],
                    'hire_date' => formatDate($employee->user?->hire_date),
                    'hire_ago' => timeAgo($employee->user?->hire_date),
                    'resign_date' => formatDate($employee->user?->resign_date),
                    'resign_ago' => timeAgo($employee->user?->resign_date),
                ],
                'referral_code' => $employee->user?->referralCode?->code,
                'outlets' => $employee->user?->outlets?->pluck('name')->toArray() ?? [],
            ])
            ->toArray();
    }

    public function updatedSearch(string $value)
    {
        $this->search = $value;

        $this->loadEmployees();
    }

    public function updatedStatus()
    {
        $this->loadEmployees();
    }

    public function delete(User $user)
    {
        $user->delete();

        $this->toast('Pegawai berhasil dihapus.');

        Flux::modals()->close();
    }

    public function render()
    {
        return view('livewire.studio.master.user.index', [
            'pageTitle' => 'Pegawai',
        ]);
    }
}
