<?php declare(strict_types=1);

namespace App\Models;

use App\Enums\CaptchaLogType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class CaptchaLog extends Model
{
    use HasFactory;

    protected $table = 'captcha_logs';

    const UPDATED_AT = null;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'captcha_id',
        'type',
        'ip',
        'user_agent',
        'referer',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'type' => CaptchaLogType::class,
        ];
    }

    public function scopeLatest(Builder $query): Builder
    {
        return $query->orderBy('created_at', 'desc');
    }

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