<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ToolStats extends Model
{
    use HasFactory;
    protected $table = "tool_stats";
    protected $fillable = [
        'created_at',
        'created_by_id',
        'created_by_ip',
        'updated_at',
        'updated_by_id',
        'updated_by_ip',
        'name',
        'value',
    ];

    public function processNew(
        string $tool
    ): bool {
        $process = DB::table('tool_stats')
            ->where('name', $tool)
            ->first();

        DB::table('tool_stats')
            ->where('name', $tool)
            ->update([
                'value' => $process->value + 1,
            ]);

        return true;
    }

    public function processCount(): array
    {
        $processCounts = DB::table('tool_stats')
            ->get();

        $counts = [];
        foreach ($processCounts as $processCount) {
            $counts[$processCount->name] = $processCount->value;
        }

        return $counts;
    }

    public function clear(): bool {
        DB::table('tool_stats')
            ->update([
                'value' => 0,
            ]);

        return true;
    }

}
