<?php

namespace Modules\Auth\Entities;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;

class PhoneVerificationOtp extends Model
{
    use HasUuids;

    protected $table = 'phone_verification_otps';

    protected $fillable = [
        'phone_number',
        'code',
        'expires_at',
        'attempts',
        'verified_at',
        'verification_token',
        'verification_token_expires_at',
    ];

    protected function casts(): array
    {
        return [
            'expires_at' => 'datetime',
            'attempts' => 'integer',
            'verified_at' => 'datetime',
            'verification_token_expires_at' => 'datetime',
        ];
    }

    public function isExpired(): bool
    {
        return $this->expires_at->isPast();
    }

    public function hasExceededMaxAttempts(): bool
    {
        return $this->attempts >= (int) config('auth.phone_verification.max_attempts', 5);
    }

    public function isVerificationTokenExpired(): bool
    {
        return $this->verification_token_expires_at === null
            || $this->verification_token_expires_at->isPast();
    }
}
