<?php
namespace App\Models;
use Core\Model;

class ApiToken extends Model
{
    protected $table = 'api_tokens';

    public function insert($token, $expired_at)
    {
        $sql = "INSERT INTO {$this->table} (token, expired_at) VALUES (?, ?)";
        return $this->query($sql, [$token, $expired_at]);
    }

    public function isValid($token)
    {
        $sql = "SELECT COUNT(*) as cnt FROM {$this->table} WHERE token = ? AND expired_at > NOW()";
        $stmt = $this->query($sql, [$token]);
        $row = $stmt->fetch();
        return $row['cnt'] > 0;
    }
}
