<?php

namespace App\Services\EmergencyContacts;

use App\Models\EmergencyContact;
use App\Services\PhoneFormatterService;

class EmergencyContactCrudService
{
    public function __construct(
        private PhoneFormatterService $phoneFormatter
    ) {
    }

    public function update(int $contactId, array $payload): array
    {
        $contact = EmergencyContact::query()->findOrFail($contactId);

        $contact->update([
            'emergency_contact_name' => $payload['name'],
            'cellphone' => $this->phoneFormatter->formatPhoneNumber($payload['cellphone']),
            'email' => $payload['email'] ?? null,
            'relation' => $payload['relation'] ?? null,
        ]);

        return $contact->toArray();
    }

    public function delete(int $contactId): void
    {
        $contact = EmergencyContact::query()->findOrFail($contactId);
        $contact->delete();
    }
}
