<?php

namespace App\Models;

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

class GuestData extends BaseModel
{
    use HasFactory;

    protected $table = 'guest_data';

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

    public function getUserID(){
        $detail = (array) $this->detailJson(true);
        return @$detail['user_id'];
    }

    public function user(){
        $userID = $this->getUserID();
        if(!$userID) return null;
        return User::where('id', $userID)->first();
    }

}
