<?php

namespace App\Livewire\Forms\Studio\Setting;

use App\Rules\PhoneNumber;
use App\Settings\ContactSettings;
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', 'min:10', 'max:20', new PhoneNumber],
            'email' => ['nullable', 'email', 'max:100'],
            'address' => ['required', 'string', 'min:10', 'max:500'],
            'map_embed' => ['nullable', 'string', 'max:2000'],
        ];
    }

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

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

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

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

        $settings->save();
    }
}
