<?php

namespace App\Models;

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

class MemberAccessLog extends Model
{
    use HasFactory;

    protected $fillable = [
        'member_id',
        'scanned_by_id',
        'token',
        'action',
        'result',
        'deny_reason',
        'note',
        'scanned_at',
    ];

    protected function casts(): array
    {
        return [
            'member_id'     => 'integer',
            'scanned_by_id' => 'integer',
            'scanned_at'    => 'datetime',
        ];
    }

    public function member()
    {
        return $this->belongsTo(Member::class);
    }

    public function scannedBy()
    {
        return $this->belongsTo(Security::class, 'scanned_by_id');
    }

    public function scopeForMember($query, int $memberId)
    {
        return $query->where('member_id', $memberId);
    }

    public function scopeToday($query)
    {
        return $query->whereDate('scanned_at', now()->toDateString());
    }

    public function scopeGranted($query)
    {
        return $query->where('result', 'granted');
    }
}
