<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;

class TestResult extends BaseModel
{
    use HasFactory;

    protected $table = 'test_result';

    public function storeOrder(){
        return $this->hasOne(StoreOrder::class, 'id', 'store_order_id');
    }

    public function detailJson($toArray = false)
    {
        if($toArray){
            return json_decode($this->detail_json ?? '{}', true);
        }
        return json_decode($this->detail_json ?? '{}');
    }

    public function getDetailJsonValue($field)
    {
        $parsed = $this->detailJson(true);
        if (isset($parsed[$field])) {
            return $parsed[$field];
        }
        return null;
    }
}
