<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\MailImport\Entity\Status\Import;

class MailAddressPair
{
    /** @var string */
    private $_sourceEmail;
    /** @var string */
    private $_targetMailname;
    /** @var \Plesk\MigrationLibV8\MailImport\Entity\Status\Issue[] */
    private $_issues;
    /** @var string|null */
    private $_status;
    /** @var string|null */
    private $_step;
    /** @var string|null */
    private $_errorId;
    /** @var string|null */
    private $_errorMessage;
    /** @var int|null */
    private $_restoredMessageNum;

    /**
     * MailAddressPair constructor.
     *
     * @param string $sourceEmail
     * @param string $targetMailname
     * @param \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[] $issues
     * @param string|null $status
     * @param string|null $step
     * @param string|null $errorId
     * @param string|null $errorMessage
     * @param int|null $restoredMessageNum
     */
    public function __construct(
        $sourceEmail, $targetMailname, $issues,
        $status=null, $step=null, $errorId=null, $errorMessage=null, $restoredMessageNum=null
    ) {
        $this->_sourceEmail = $sourceEmail;
        $this->_targetMailname = $targetMailname;
        $this->_issues = $issues;
        $this->_status = $status;
        $this->_step = $step;
        $this->_errorId = $errorId;
        $this->_errorMessage = $errorMessage;
        $this->_restoredMessageNum = $restoredMessageNum;
    }

    /**
     * Get source e-mail (e.g. "john@example.com").
     *
     * @return string
     */
    public function getSourceEmail()
    {
        return $this->_sourceEmail;
    }

    /**
     * Get target mailname (a part of e-mail before "@", e.g. "john")
     *
     * @return string
     */
    public function getTargetMailname()
    {
        return $this->_targetMailname;
    }

    /**
     * Get list of issues happened during import.
     *
     * @return \Plesk\MigrationLibV8\MailImport\Entity\Status\Issue[]
     */
    public function getIssues()
    {
        return $this->_issues;
    }

    /**
     * Get import status of the mail address pair, as string (e.g. "success")
     *
     * @return string|null see constants from \Plesk\MigrationLibV8\MailImport\Entity\Status\Import\Statuses
     */
    public function getStatus()
    {
        return $this->_status;
    }

    /**
     * Get step on which migration is right now (if import is in progress).
     *
     * @return string|null
     */
    public function getStep()
    {
        return $this->_step;
    }

    /**
     * Get error identifier, as string.
     *
     * @return string|null
     */
    public function getErrorId()
    {
        return $this->_errorId;
    }

    /**
     * Get error message, as string.
     *
     * @return string|null
     */
    public function getErrorMessage()
    {
        return $this->_errorMessage;
    }

    /**
     * Get number of message which is restored by migrator backend right now.
     *
     * @return int|null
     */
    public function getRestoredMessageNum()
    {
        return $this->_restoredMessageNum;
    }

    /**
     * Return status information for imported mail address pairs as array (useful for integration with JavaScript code).
     *
     * Array has the following keys:
     *    'source_email' - source e-mail address (e.g. "source@example.com")
     *    'target_mailname' - target mailname - part of e-mail address
     *        before "@" (e.g. "target" for "target@example.com" e-mail)
     *    'status' - import status of the mail address pair, as string (e.g. "success")
     *    'step' - step on which import is running, if it is in progress, as string
     *    'issues' - list of issues, as array, for example:
     *      [
     *          [
     *              'severity' => 'error',
     *              'execution_date' => 1520414386.50899,
     *              'problem_text' => 'Failed to copy content',
     *              'details_text' => 'Failed to execute command, non-zero exit code'
     *          ],
     *          [
     *              'severity' => 'warning',
     *              'execution_date' => 1520414234.31467,
     *              'problem_text' => 'Password does not conform to Plesk rules, new password was generated',
     *              'details_text' => null
     *          ],
     *      ]
     *    'error_id' - error identifier, as string (use to perform special actions in case of specific error, e.g.
     *              highlight login/password fileds in case of authentication issue, and so on)
     *    'error_message' - human readable error message
     *    'restored_message_num' - number of a message which is restored by migrator backend right now (e.g. 42)
     * @return array
     */
    public function asArray()
    {
        $issues = [];
        foreach ($this->getIssues() as $issue) {
            $issues[] = $issue->asArray();
        }

        return [
            'source_email' => $this->getSourceEmail(),
            'target_mailname' => $this->getTargetMailname(),
            'issues' => $issues,
            'status' => $this->getStatus(),
            'step' => $this->getStep(),
            'error_id' => $this->getErrorId(),
            'error_message' => $this->getErrorMessage(),
            'restored_message_num' => $this->getRestoredMessageNum(),
        ];
    }
}
