<?php

namespace Podeli\Payment\Models;

class ClientInfo extends Entity
{
    public function setFirstName(string $firstName)
    {
        $this->data['firstName'] = $firstName;
        return $this;
    }

    public function setLastName(string $lastName)
    {
        $this->data['lastName'] = $lastName;
        return $this;
    }

    public function setMiddleName($middleName)
    {
        $this->data['middleName'] = $middleName;
        return $this;
    }

    public function setPhone($phone)
    {
        $phone = $this->normalizePhone($phone);

        $this->data['phone'] = $phone;
        return $this;
    }

    public function getPhone():string
    {
        return $this->data['phone'] ?? '';
    }

    public function setEmail($email)
    {
        $this->validateEmail($email);
        $this->data['email'] = $email;
        return $this;
    }

    public function normalizePhone(string $phone): string
    {
        $phone = preg_replace("#[^\d]#", "", $phone);
        if (!preg_match("#[7|8]{0,1}(9\d{9})#", $phone, $match)) {
            return '';
        }
        return '7' . $match[1];
    }

    public function validateEmail(string $email)
    {
        if (strpos($email, '@') === false) {
            throw new \Exception('Wrong email');
        }
    }
}
