<?php

namespace ZenoChatbot\Services\LLMService\AIClient\Support\Handlers;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Handles file upload resources ($_FILES style arrays).
 */
class UploadHandler
{
    /**
     * Load a file from upload array
     */
    public static function load(array $upload, array $options = []): array
    {
        $tmp = (string) ($upload['tmp_name'] ?? '');
        $name = (string) ($upload['name'] ?? 'upload');
        $type = (string) ($upload['type'] ?? FileHandler::inferTypeFromFilename($name));

        if ($tmp && is_readable($tmp)) {
            $raw = @file_get_contents($tmp) ?: '';
            $text = TextHandler::coerceToText($raw, $type, $options);

            return [
                'id' => md5($name . '|' . $tmp),
                'name' => $name,
                'type' => $type,
                'text' => $text,
                'tmp_name' => $tmp,
            ];
        }

        return [
            'id' => md5($name . '|' . $tmp),
            'name' => $name,
            'type' => $type,
            'text' => '',
            'tmp_name' => $tmp,
        ];
    }
}

