<?php

namespace Modules\NX600Lab\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class NxCommunicationLog extends Model
{
    use SoftDeletes;

    protected $table = 'nx_communication_logs';

    protected $fillable = [
        'level', 'category', 'device_ip', 'sample_no', 'message', 'context',
    ];

    protected $casts = [
        'context' => 'array',
    ];

    protected static function write(string $level, string $message, string $category, array $context, ?string $deviceIp, ?string $sampleNo): void
    {
        static::create([
            'level'     => $level,
            'category'  => $category,
            'message'   => $message,
            'context'   => $context,
            'device_ip' => $deviceIp,
            'sample_no' => $sampleNo,
        ]);
    }

    public static function info(string $message, string $category = 'system', array $context = [], ?string $deviceIp = null, ?string $sampleNo = null): void
    {
        static::write('info', $message, $category, $context, $deviceIp, $sampleNo);
    }

    public static function success(string $message, string $category = 'system', array $context = [], ?string $deviceIp = null, ?string $sampleNo = null): void
    {
        static::write('success', $message, $category, $context, $deviceIp, $sampleNo);
    }

    public static function warning(string $message, string $category = 'system', array $context = [], ?string $deviceIp = null, ?string $sampleNo = null): void
    {
        static::write('warning', $message, $category, $context, $deviceIp, $sampleNo);
    }

    public static function error(string $message, string $category = 'system', array $context = [], ?string $deviceIp = null, ?string $sampleNo = null): void
    {
        static::write('error', $message, $category, $context, $deviceIp, $sampleNo);
    }

    public function getLevelBadgeAttribute(): string
    {
        return match ($this->level) {
            'success' => 'bg-success',
            'warning' => 'bg-warning text-dark',
            'error'   => 'bg-danger',
            default   => 'bg-info text-dark',
        };
    }

    public function getCategoryIconAttribute(): string
    {
        return match ($this->category) {
            'connection' => 'fa fa-plug',
            'worklist'   => 'fa fa-list',
            'result'     => 'fa fa-flask',
            'error'      => 'fa fa-exclamation-triangle',
            default      => 'fa fa-cog',
        };
    }
}
