<?php

namespace App\Models;

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

class OrderAreaScanEvent extends Model
{
    use HasFactory;

    protected $table = 'order_area_scan_events';

    public const SEQUENCE_FIRST = 1;
    public const SEQUENCE_SECOND = 2;

    protected $fillable = [
        'orden_lavanderia_id',
        'area_id',
        'scan_sequence',
        'user_id',
        'scanned_at',
        'device_label',
        'raw_payload',
        'client_ip_hash',
    ];

    protected $casts = [
        'scan_sequence' => 'integer',
        'scanned_at' => 'datetime',
        'raw_payload' => 'array',
    ];

    public function ordenLavanderia(): BelongsTo
    {
        return $this->belongsTo(OrdenLavanderia::class, 'orden_lavanderia_id');
    }

    public function area(): BelongsTo
    {
        return $this->belongsTo(Area::class, 'area_id');
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function scopeForOrderAndArea($query, int $ordenLavanderiaId, int $areaId)
    {
        return $query->where('orden_lavanderia_id', $ordenLavanderiaId)
            ->where('area_id', $areaId);
    }

    public function scopeBetweenDates($query, $from, $to)
    {
        return $query->whereBetween('scanned_at', [$from, $to]);
    }
}
