<?php
namespace EasyChat\Library\Messenger;







class MaxAccountProfile {

    public static function fromProfile($profile): array {
        $contact = is_object($profile) && isset($profile->contact) ? $profile->contact : null;
        if (!is_object($contact)) {
            return [];
        }

        $name = '';
        foreach ((array)($contact->names ?? []) as $candidate) {
            $value = trim((string)($candidate->name ?? ''));
            if ($value === '') {
                $value = trim(trim((string)($candidate->firstName ?? '')) . ' ' . trim((string)($candidate->lastName ?? '')));
            }
            $name = self::cleanName($value);
            if ($name !== '') {
                break;
            }
        }
        if ($name === '') {
            $name = 'Личный MAX';
        }

        return [
            'display_name' => $name,
            'initials' => self::initials($name),
            'phone_mask' => self::maskPhone($contact->phone ?? ''),
            'updated_at' => time(),
        ];
    }

    public static function normalize(array $profile): array {
        $name = self::cleanName((string)($profile['display_name'] ?? ''));
        if ($name === '') {
            $name = 'Личный MAX';
        }
        $phone_mask = trim((string)($profile['phone_mask'] ?? ''));
        if (!preg_match('/^[+*•\d\s()\-]{0,32}$/u', $phone_mask)) {
            $phone_mask = '';
        }
        return [
            'display_name' => $name,
            'initials' => self::initials($name),
            'phone_mask' => self::slice($phone_mask, 32),
            'updated_at' => max(0, (int)($profile['updated_at'] ?? 0)),
        ];
    }

    private static function maskPhone($value): string {
        if (is_array($value)) {
            $value = $value['phone'] ?? $value['number'] ?? $value['value'] ?? '';
        } elseif (is_object($value)) {
            $value = $value->phone ?? $value->number ?? $value->value ?? '';
        }
        if (!is_scalar($value)) {
            return '';
        }
        $digits = preg_replace('/\D+/', '', (string)$value);
        if (!is_string($digits) || strlen($digits) < 4) {
            return '';
        }
        return '+' . substr($digits, 0, 1) . ' ••• •••-' . substr($digits, -4);
    }

    private static function cleanName(string $name): string {
        $name = trim(preg_replace('/\s+/u', ' ', $name) ?: '');
        if ($name === '' || preg_match('/^\d+$/', $name)) {
            return '';
        }
        return self::slice(strip_tags($name), 80);
    }

    private static function initials(string $name): string {
        $parts = preg_split('/\s+/u', trim($name)) ?: [];
        $initials = '';
        foreach (array_slice($parts, 0, 2) as $part) {
            $letter = self::slice($part, 1);
            $initials .= function_exists('mb_strtoupper') ? mb_strtoupper($letter, 'UTF-8') : strtoupper($letter);
        }
        return $initials !== '' ? $initials : 'M';
    }

    private static function slice(string $value, int $length): string {
        if (function_exists('mb_substr')) {
            return mb_substr($value, 0, $length, 'UTF-8');
        }
        $characters = preg_split('//u', $value, -1, PREG_SPLIT_NO_EMPTY);
        return is_array($characters) ? implode('', array_slice($characters, 0, $length)) : substr($value, 0, $length);
    }
}
