<?php
namespace App\Services;

use App\Mail\MailSyS;
use App\Models\Logger;
use Illuminate\Support\Facades\Mail;


class SysLog
{


   /* protected $user_id;
    protected $model;
    protected $model_id;
    protected $message;
    protected $action;
    protected $channel;
    protected $level;*/

    protected $log;

    public $levelTypes = [
        1 => 'debug',
        2 => 'info',
        3 => 'notice',
        4 => 'warning',
        5 => 'error',
        6 => 'critical',
        7 => 'alert',
    ];

    function __construct($action = null, $channel = 'default', $level = 1)
    {
        $this->log = new Logger();
        $this->log->action = $action;
        $this->log->channel = $channel;
        $this->log->level = $level;
    }

    public static function action($action = null, $channel = 'default', $level = 1)
    {
        //Return new instance of this model
        return new self($action, $channel, $level);
    }

    public function setModel($id, $model){
        $this->log->model_id = $id;
        $this->log->model = $model;
        return $this;
    }

    public function setUserId($user_id){
        $this->log->user_id = $user_id;
        return $this;
    }

    public function setMessage($message){
        $this->log->message = $message;
        return $this;
    }

    public function save(){
        $this->log->save();
        //send Mail
        if($this->log->level >= 3){
            $mail = config('app.info_test_mail');
            Mail::to($mail)->send(new MailSyS($this->log, 'log'));
        }
    }

    public function getLevelType(){
       return $this->levelTypes[$this->log->level];
    }



}
