<?php

namespace App\Models\Monitoring;

use App\Console\Trace;
use App\Models\Monitoring\Log;
use App\Models\Environments\Environment;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Notice extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'notices';

    protected $fillable = [
        'environment_id', 'model', 'method', 'type',
        'request', 'response', 'response_id', 'status'
    ];

    protected $casts = [
        'request' => 'array',
        'response' => 'array',
        'status' => 'integer',
    ];

    public $status_string = [
        0 => 'Reseteado',
        1 => 'Enviado',
        2 => 'Error'
    ];

    public function environment()
    {
        return $this->belongsTo(Environment::class);
    }

    /** Mantengo tu lógica personalizada intacta */

    public static function log(array $params)
    {
        $trace = new Trace('systems_updates.log');
        $search = [
            'environment_id' => $params['environment_id'],
            'model'   => $params['model'],
            'method'  => $params['method'],
            'type'    => $params['type'],
        ];

        $notice = DB::table('notices')->where($search)->first();

        if ($notice) {
            $trace->echo('INFO', 'Actualizar Notice: ' . $notice->id);
            self::log_update($notice->id, $params);
        } else {
            $trace->echo('INFO', 'Crear Notice');
            self::log_insert($params);
        }
    }

    public static function log_insert(array $params)
    {
        try {
            self::create($params);
        } catch (\Exception $e) {
            Log::db('warning', '80001', 'Notice create', 'environment', $params['environment_id'], $e->getMessage());
        }
    }

    public static function log_update(int $id, array $params)
    {
        $notice = self::find($id);

        if ($notice) {
            try {
                $notice->update($params);
            } catch (\Exception $e) {
                Log::db('warning', '80101', 'Notice update', 'environment', $params['environment_id'], $e->getMessage());
            }
        } else {
            Log::db('warning', '80102', 'Notice update', 'notice', $id, 'Notice not found');
        }
    }

    public static function reset(int $environment_id, string $model, string $place)
    {
        try {
            $reset = [
                'actions' => 'reset',
                'date' => now(),
                'user_id' => Auth::id(),
                'place' => $place,
            ];

            DB::table('notices')
                ->where(['environment_id' => $environment_id, 'model' => $model])
                ->update([
                    'status' => 0,
                    'response' => json_encode($reset, JSON_PRETTY_PRINT),
                ]);
        } catch (\Exception $e) {
            Log::db('warning', '80201', 'Notice reset', 'Environment', $environment_id, $e->getMessage());
        }
    }
}

