<?php
namespace App\Models;

use CodeIgniter\Model;

class AuthModel extends Model
{
    protected $table = 'user_otp';
    protected $allowedFields = ['mobile', 'otp', 'email_otp', 'expires_at'];
    protected $useTimestamps = false;

    public function saveOtp($mobile, $otp, $email_otp, $expiry)
    {
        // Remove old OTP for this mobile
        $this->where('mobile', $mobile)->delete();

        return $this->insert([
            'mobile'     => $mobile,
            'otp'        => $otp,
            'email_otp'  => $email_otp,
            'expires_at' => $expiry
        ]);
    }

    public function verifyOtp($mobile, $otp)
    {
        return $this->where('mobile', $mobile)
                    ->where('otp', $otp)
                    ->where('expires_at >=', date('Y-m-d H:i:s'))
                    ->first();
    }
}
