<?php

namespace auctionforge\inc\model;

use auctionforge\inc\abs\Model;
use auctionforge\inc\InstanceDB;

class FirebaseLog extends Model
{
    const TABLE_APP_LOGS = "bp_firebase_logs";
    const STATUS_NONE = 'none';
    const STATUS_SENT = 'sent';
    const STATUS_ERROR = 'error';

    private $created;
    private $firebaseId;
    private $bidspiritUserEmail;
    private $bidspiritLotId;
    private $requestFromBidspirit;
    private $requestToFirebase;
    private $responseFromFirebase;
    private $responseToBidspirit;
    private $status;

    /**
     * @param $status
     */
    public function __construct($status)
    {
        $this->setStatus($status);
    }


    public function loadAttributes($params) {
        $this->setCreated(time());
        parent::loadAttributes($params);
    }

    public function save()
    {
        $db = InstanceDB::getInstanceDB();
        $sql = "INSERT INTO " . $db->prefix . self::TABLE_APP_LOGS . " (
        `firebase_id`, `bidspirit_user_email`, `bidspirit_lot_id`, `request_from_bidspirit`, `request_to_firebase`, `response_from_firebase`, `response_to_bidspirit`, `status`
        ) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
        $sql = $db->prepare($sql,
            $this->getFirebaseId(),
            $this->getBidspiritUserEmail(),
            $this->getBidspiritLotId(),
            $this->getRequestFromBidspirit(),
            $this->getRequestToFirebase(),
            $this->getResponseFromFirebase(),
            $this->getResponseToBidspirit(),
            $this->getStatus()
        );
        return $db->query($sql);
    }

    /**
     * @return mixed
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * @param  mixed  $created
     */
    public function setCreated($created)
    {
        $this->created = $created;
    }

    /**
     * @return mixed
     */
    public function getFirebaseId()
    {
        return $this->firebaseId;
    }

    /**
     * @param  mixed  $firebaseId
     */
    public function setFirebaseId($firebaseId)
    {
        $this->firebaseId = $firebaseId;
    }

    /**
     * @return mixed
     */
    public function getBidspiritUserEmail()
    {
        return $this->bidspiritUserEmail;
    }

    /**
     * @param  mixed  $bidspiritUserEmail
     */
    public function setBidspiritUserEmail($bidspiritUserEmail)
    {
        $this->bidspiritUserEmail = $bidspiritUserEmail;
    }

    /**
     * @return mixed
     */
    public function getBidspiritLotId()
    {
        return $this->bidspiritLotId;
    }

    /**
     * @param  mixed  $bidspiritLotId
     */
    public function setBidspiritLotId($bidspiritLotId)
    {
        $this->bidspiritLotId = $bidspiritLotId;
    }

    /**
     * @return mixed
     */
    public function getRequestFromBidspirit()
    {
        return $this->requestFromBidspirit;
    }

    /**
     * @param  mixed  $requestFromBidspirit
     */
    public function setRequestFromBidspirit($requestFromBidspirit)
    {
        $this->requestFromBidspirit = $this->encode($requestFromBidspirit);
    }

    /**
     * @return mixed
     */
    public function getRequestToFirebase()
    {
        return $this->requestToFirebase;
    }

    /**
     * @param  mixed  $requestToFirebase
     */
    public function setRequestToFirebase($requestToFirebase)
    {
        $this->requestToFirebase = $this->encode($requestToFirebase);
    }

    /**
     * @return mixed
     */
    public function getResponseFromFirebase()
    {
        return $this->responseFromFirebase;
    }

    /**
     * @param  mixed  $responseFromFirebase
     */
    public function setResponseFromFirebase($responseFromFirebase)
    {
        $this->responseFromFirebase = $this->encode($responseFromFirebase);
    }

    /**
     * @return mixed
     */
    public function getResponseToBidspirit()
    {
        return $this->responseToBidspirit;
    }

    /**
     * @param  mixed  $responseToBidspirit
     */
    public function setResponseToBidspirit($responseToBidspirit)
    {
        $this->responseToBidspirit = $this->encode($responseToBidspirit);
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param  mixed  $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    private function encode($data)
    {
        return json_encode($data);
    }
}