<?php

namespace App\Repositories;

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

class AttachmentRepository
{
    public function getAllAttachmentsByProcess(int $processId): array
    {
        $db = Database::getConnection();

        $query = "SELECT a.* FROM attachments a
                  JOIN locations l ON a.location_id = l.id
                  WHERE l.process_id = ?";

        $stmt = $db->prepare($query);
        $stmt->execute([$processId]);

        $attachments = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $attachment = new Attachment();
            $attachment->id = $row['id'];
            $attachment->location_id = $row['location_id'];
            $attachment->file_name = $row['file_name'];
            $attachment->file_path = $row['file_path'];
            $attachment->created_at = $row['created_at'];
            $attachments[] = $attachment;
        }

        return $attachments;
    }
}