<?php

namespace App\Livewire\Forms\Member\Setting;

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

class ProfileForm extends Form
{
    public ?Customer $customer = null;

    public string $name = '';

    public string $phone_number = '';

    public string $gender = '';

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:100'],
            'phone_number' => ['required', 'string', new PhoneNumber],
            'gender' => ['required', Rule::in(Gender::cases())],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'name' => 'nama lengkap',
            'phone_number' => 'nomor telepon',
            'gender' => 'jenis kelamin',
        ];
    }

    public function setProfile(Customer $customer): void
    {
        $this->customer = $customer;

        $this->name = $customer->name;
        $this->phone_number = $customer->phone_number;
        $this->gender = $customer->gender->value;
    }

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

        $this->customer->update([
            'name' => $this->name,
            'phone_number' => $this->phone_number,
            'gender' => $this->gender,
        ]);
    }
}
