<?php

namespace App\Livewire\Forms\Studio\Setting;

use App\Enums\Gender;
use App\Models\Employee;
use App\Rules\PhoneNumber;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Livewire\Form;

class ProfileForm extends Form
{
    public ?Employee $employee = null;

    public string $full_name = '';

    public string $phone_number = '';

    public string $base_salary = '';

    public string $birthdate = '';

    public string $hire_date = '';

    public string $address = '';

    public string $gender = '';

    public string $status = '';

    public function rules(): array
    {
        return [
            'full_name' => ['required', 'string', 'max:100'],
            'phone_number' => ['required', 'string', new PhoneNumber],
            'birthdate' => ['required', 'date', 'before:'.Carbon::now()->subYears(18)->format('Y-m-d')],
            'address' => ['required', 'string'],
            'gender' => ['required', Rule::in(Gender::cases())],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'full_name' => 'nama lengkap',
            'phone_number' => 'nomor telepon',
            'base_salary' => 'gaji pokok',
            'birthdate' => 'tanggal lahir',
            'address' => 'alamat',
            'gender' => 'jenis kelamin',
        ];
    }

    public function setProfile(Employee $employee)
    {
        $this->employee = $employee;

        $this->full_name = $employee->full_name;
        $this->phone_number = $employee->phone_number;
        $this->base_salary = $employee->base_salary;
        $this->birthdate = $employee->birthdate;
        $this->hire_date = formatDate($employee->hire_date);
        $this->address = $employee->address;
        $this->gender = $employee->gender->value;
        $this->status = $employee->status->label();
    }

    public function update()
    {
        $this->validate();

        $this->employee->update([
            'full_name' => $this->full_name,
            'phone_number' => $this->phone_number,
            'birthdate' => $this->birthdate,
            'address' => $this->address,
            'gender' => $this->gender,
        ]);
    }
}
