<?php

namespace App\Livewire\Forms\Studio\Setting;

use App\Rules\PhoneNumber;
use App\Settings\ContactSettings;
use Illuminate\Validation\Rule;
use Livewire\Form;

class ContactForm extends Form
{
    public string $phone_number = '';

    public ?string $email = null;

    public string $address = '';

    public ?string $map_embed = null;

    public function rules(): array
    {
        return [
            'phone_number' => ['required', 'string', new PhoneNumber],
            'email' => ['nullable', 'email', 'max:100', Rule::unique('users', 'email')],
            'address' => ['required', 'string', 'max:255'],
            'map_embed' => ['nullable', 'string'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'phone_number' => 'nomor telepon',
            'address' => 'alamat',
            'map_embed' => 'map embed',
        ];
    }

    public function setSettings(ContactSettings $settings): void
    {
        $this->address = $settings->address;
        $this->phone_number = $settings->phone_number;
        $this->email = $settings->email;
        $this->map_embed = $settings->map_embed;
    }

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

        $settings->address = $this->address;
        $settings->phone_number = $this->phone_number;
        $settings->email = $this->email;
        $settings->map_embed = $this->map_embed;

        $settings->save();
    }
}
