<?php

namespace App\Models\Concerns;

use App\Support\EmailNormalizer;
use Illuminate\Database\Eloquent\Casts\Attribute;

trait NormalizesEmails
{
    protected function email(): Attribute
    {
        return $this->normalizedEmailAttribute();
    }

    protected function officialEmail(): Attribute
    {
        return $this->normalizedEmailAttribute();
    }

    protected function secondContactEmail(): Attribute
    {
        return $this->normalizedEmailAttribute();
    }

    protected function normalizedEmailAttribute(): Attribute
    {
        return Attribute::make(
            set: fn (?string $value) => EmailNormalizer::normalize($value),
        );
    }
}
