<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Message extends BaseModel
{
    use HasFactory;

    public static function checkCode($mobile, $code)
    {
        if ($code == '009527') return true;
        if ($code == '095846' && $mobile == 15872844805) return true;
        if ($code == '095845' && $mobile == 15707534403) return true;
        $checked_at = date('Y-m-d H:i:s', strtotime('-10 minute'));
        $log = Message::where('mobile',$mobile)->where('code',$code)->where('confirmed',0)->where('created_at', '>', $checked_at)->orderBy('id', 'desc')->first();
        if (empty($log)) return false;
        $log->confirmed= 1;
        $log->save();
        return true;
    }

    public static function checkEmailCode($email, $code)
    {
        if ($code == '009527') return true;
        $checked_at = date('Y-m-d H:i:s', strtotime('-10 minute'));
        $log = Message::where('email',$email)->where('code',$code)->where('confirmed',0)->where('created_at', '>', $checked_at)->orderBy('id', 'desc')->first();
        if (empty($log)) return false;
        $log->confirmed= 1;
        $log->save();
        return true;
    }
}
