<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SerialNumberCheck extends Model
{
    use HasFactory;

    const STATUS_FAIL = 0;
    const STATUS_COMPLETE = 1;
    const STATUS_PENDING = 2;

    public function __construct()
    {
        $this->table = 'serial_number_check';
        $this->casts = [
            'keyword' => 'json',
            'response' => 'json',
        ];
        $this->guarded = [];
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    public function tracking(): HasMany
    {
        return $this->hasMany(
            related: SerialNumberCheckTracking::class,
        );
    }

    public static function getHistoryWithUserInfo()
    {
        return self::select(
            'serial_number_check.*',
            'users.email as email'
        )->leftJoin('users', 'users.id', '=', 'serial_number_check.user_id');
    }
}
