<?php

namespace App\Models;

use App\Core\Model;

class OrderAttachment extends Model
{
    protected $table = 'order_attachments';

    /**
     * Retorna todos os anexos de uma ordem específica.
     */
    public function findByOrderId(int $orderId): array
    {
        $stmt = $this->conn->prepare(
            "SELECT * FROM {$this->table} WHERE order_id = :order_id ORDER BY created_at ASC"
        );
        $stmt->execute(['order_id' => $orderId]);
        return $stmt->fetchAll();
    }

    /**
     * Insere um registro de anexo no banco.
     */
    public function create(array $data): int
    {
        $stmt = $this->conn->prepare(
            "INSERT INTO {$this->table} (order_id, original_name, stored_name, mime_type, size)
             VALUES (:order_id, :original_name, :stored_name, :mime_type, :size)"
        );
        $stmt->execute([
            'order_id'      => $data['order_id'],
            'original_name' => $data['original_name'],
            'stored_name'   => $data['stored_name'],
            'mime_type'     => $data['mime_type'] ?? null,
            'size'          => $data['size'] ?? null,
        ]);
        return (int) $this->conn->lastInsertId();
    }
}
