<?php

namespace App\Models\Sistema;

use App\Models\BaseModel;

class ActivityModel extends BaseModel
{
    protected $table = 'auth_activity';
    protected $primaryKey = 'id';

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;

    protected $returnType = "App\Entities\Sistema\ActivityEntity";

    const SORTABLE = [
        1 => "t1.id",
        2 => "t2.username",
        3 => "t1.level",
        4 => "t1.event",
        5 => "t1.ip",
        6 => "t1.os",
        7 => "t1.browser",
        8 => "t1.detail"
    ];

    protected $allowedFields = [
        'user_id',
        'level',
        'event',
        'ip',
        'os',
        'browser',
        'detail'
    ];

    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';


    /**
     * Retrieves a resource from the database based on the given search string.
     *
     * @param string $search The search string to filter the resource by. Defaults to an empty string.
     * @return mixed The resource query builder instance if search string is empty, otherwise the filtered resource query builder instance.
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t2.username AS user, t1.level AS level, t1.event AS event, t1.ip AS ip, t1.os AS os, 
                t1.browser AS browser, t1.created_at AS created_at"
            )
            ->join("users t2", "t1.user_id = t2.id", "left")
            ->orderBy('t1.created_at', 'DESC');

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t2.username", $search)
                ->orLike("t1.level", $search)
                ->orLike("t1.event", $search)
                ->orLike("t1.ip", $search)
                ->orLike("t1.os", $search)
                ->orLike("t1.browser", $search)
                ->orLike("t1.created_at", $search)
                ->groupEnd();
    }

    public function getLogs()
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                'SUM( IF( os LIKE "%Windows%", 1, 0 ) ) AS windows,
                SUM( IF( os = "Mac OS X", 1, 0 ) ) AS mac,
                SUM( IF( os = "Linux", 1, 0 ) ) AS linux,
                SUM( IF( os = "Android", 1, 0 ) ) AS android,
                SUM( IF( os = "iOS", 1, 0 ) ) AS iphone,
                SUM( IF( browser LIKE "%Chrome%", 1, 0 ) ) AS chrome,
                SUM( IF( browser LIKE "%Firefox%", 1, 0 ) ) AS firefox,
                SUM( IF( browser LIKE "%Safari%", 1, 0 ) ) AS safari,
                SUM( IF( browser LIKE "%Internet Explorer%", 1, 0 ) ) AS ie,
                SUM( IF( browser LIKE "%Edge%", 1, 0 ) ) AS edge,
                SUM( IF( browser LIKE "%Opera%", 1, 0 ) ) AS opera'
            );

        return $builder;
    }

}