<?php

namespace App\Repositories;

use App\Models\Response;
use Config\Database;
use PDO;

class ResponseRepository
{
    public function getResponsesForUser($userId) {
        $db = Database::getConnection();
        $query = "SELECT r.* 
                  FROM responses r 
                  JOIN processes p ON r.process_id = p.id 
                  WHERE p.user_id = :user_id 
                  ORDER BY r.created_at DESC";
        
        $stmt = $db->prepare($query);
        $stmt->execute([':user_id' => $userId]);
    
        $responses = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $responses[] = new Response($row);
        }
    
        return $responses;
    }

    public function markResponseAsRead($responseId) {
        $db = Database::getConnection();
        $query = "UPDATE responses 
                  SET is_read = 1, read_at = NOW() 
                  WHERE id = :id AND is_read = 0";
        $stmt = $db->prepare($query);
        return $stmt->execute([':id' => $responseId]);
    }
}