<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\AuthEventType;
use App\Models\Concerns\AppendOnly;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Authentication and account-security history.
 *
 * Kept separate from the marketplace audit trail: this table is written on
 * every login attempt including failures, so it grows on a different curve and
 * is queried by the risk checks rather than by dispute resolution.
 */
class AuthEvent extends Model
{
    use AppendOnly;

    public const UPDATED_AT = null;

    /** @var list<string> */
    protected $fillable = [
        'user_id',
        'email_attempted',
        'event',
        'ip_address',
        'user_agent',
        'device_fingerprint',
        'country_code',
        'metadata',
    ];

    /** @return array<string, string> */
    protected function casts(): array
    {
        return [
            'event' => AuthEventType::class,
            'metadata' => 'array',
            'created_at' => 'datetime',
        ];
    }

    /** @return BelongsTo<User, $this> */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}
