<?php

namespace App\Packages\User\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
 * Class SmsLog
 * @package App\Packages\User\Models
 *
 * @property int $id
 * @property string $phone_code
 * @property string $phone_number
 * @property string $code
 * @property boolean $is_sent
 * @property boolean $is_valid
 *
 * @property string $aws_error_message
 *
 * @property string|Carbon $created_at
 * @property string|Carbon $updated_at
 */
class SmsLog extends Model
{
    /** @var string */
    protected $table = 'table_sms_log';

    /**
     * @param string $phoneCode
     * @param string $phoneNumber
     *
     * @return SmsLog
     */
    public static function createLog($phoneCode, $phoneNumber)
    {
        $phoneConfirm = new SmsLog();
        $phoneConfirm->phone_code = $phoneCode;
        $phoneConfirm->phone_number = $phoneNumber;
        $phoneConfirm->is_sent = false;
        $phoneConfirm->is_valid = false;
        $phoneConfirm->save();

        return $phoneConfirm;
    }

    /**
     * @param boolean $isValid
     * @return $this
     */
    public function setIsValid($isValid)
    {
        $this->is_valid = $isValid;
        $this->save();

        return $this;
    }

    /**
     * @param string $code
     * @return $this
     */
    public function setConfirmationCode($code)
    {
        $this->code = $code;
        $this->save();

        return $this;
    }

    /**
     * @return $this
     */
    public function acceptSent()
    {
        $this->is_sent = true;
        $this->save();

        return $this;
    }

    /**
     * @param string $awsMessage
     *
     * @return $this
     */
    public function declineSend($awsMessage)
    {
        $this->is_sent = false;
        $this->aws_error_message = $awsMessage;
        $this->save();

        return $this;
    }

    /**
     * @return HasOne
     */
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
}