<?php

namespace App\Repositories;

use Config\Database;
use PDO;

class PasswordResetRepository
{

    public function findValidToken($token)
    {
        $db = Database::getConnection();
        $stmt = $db->prepare("
            SELECT * FROM password_resets 
            WHERE token = ? 
            AND used = 0 
            AND deleted_at IS NULL 
            AND expires_at > NOW()
            LIMIT 1
        ");
        $stmt->execute([$token]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function markAsUsed($token)
    {
        $db = Database::getConnection();
        $stmt = $db->prepare("
            UPDATE password_resets 
            SET used = 1 
            WHERE token = ? AND deleted_at IS NULL
        ");
        $stmt->execute([$token]);
    }


    public function softDeleteAllByEmail($email)
    {
        $db = Database::getConnection();
        $stmt = $db->prepare("
            UPDATE password_resets 
            SET deleted_at = NOW() 
            WHERE email = ? AND deleted_at IS NULL
        ");
        $stmt->execute([$email]);
    }

}