<?php

class upload
{
    public static function PDF($file)
    {
        if (!is_array($file) || ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
            return 'No valid PDF upload was received.';
        }

        $fileSize = (int) ($file['size'] ?? 0);
        $temporaryFile = (string) ($file['tmp_name'] ?? '');
        if ($fileSize < 1 || $fileSize > 10000000 || !is_uploaded_file($temporaryFile)) {
            return 'The PDF must be between 1 byte and 10 MB.';
        }

        $fileInfo = new finfo(FILEINFO_MIME_TYPE);
        if ($fileInfo->file($temporaryFile) !== 'application/pdf') {
            return 'Only a valid PDF file is allowed.';
        }

        if (file_get_contents($temporaryFile, false, null, 0, 5) !== '%PDF-') {
            return 'The uploaded file is not a valid PDF.';
        }

        $newfilename = bin2hex(random_bytes(16)) . '.pdf';
        $uploadDirectory = dirname(__DIR__) . '/admin/product_wording';
        $destination = $uploadDirectory . '/' . $newfilename;

        if (!is_dir($uploadDirectory) || !move_uploaded_file($temporaryFile, $destination)) {
            return 'Failed to upload file. Contact the site administrator.';
        }

        return ['Upload Successfully!', $newfilename];
    }
}
