<?php

namespace Hura8\System;

use Hura8\Interfaces\AppResponse;
use Hura8\Interfaces\FileHandleResponse;
use Hura8\System\Controller\bFileHandle;

/**
 * @date 25-Oct-2023
 * Single upload handle for whole application
 * - Manage security
 * - upload file to desired destination
 * - return the file path for other modules to use
 *
 * Security:
 * - restrict file extension and mine type
 *
 * Sample code:

    $objFileUpload = new FileUpload(
        "media/banner", //.date("Y-m-d"),
        array(".jpg",".jpeg",".gif", ".png", ".webp", '.avif')
    );
    $upload_result = $objFileUpload->handleUpload("file");

    if($upload_result->getStatus() == 'ok') {
        $file_info = $upload_result->getData();

        $file_name = $file_info->file_name;
        $file_ext = $file_info->file_ext;
        $public_path = $file_info->public_path;

        $renamed = $objFileUpload->renameUploadedFile($file_name, $item_id . $file_ext);

        if($renamed) {
            $new_file_name = $renamed['file_name'];
            $new_public_path = $renamed['public_path'];
        }

    }
 *
 */
class FileUpload extends bFileHandle
{

    /**
     * @description version of handleUpload to support multiple files using array input
     * example: <input type="file" size="30" name="multiple_file[]" /> instead of <input type="file" size="30" name="single_file" />
     *
     * @param string $input_file_name name of <input type="file" size="30" name="multiple_file[]" /> => use: multiple_file
     * @param int $max_file_size max file size in bytes accepts, default = 1MB
     * @return FileHandleResponse[]
     */
    public function handleMultipleUpload(string $input_file_name, int $max_file_size = 1000000) : array
    {
        $upload_result = [];
        for($i = 0; $i < sizeof($_FILES[$input_file_name]['name']); $i++){
            $original_uploaded_file_name = $_FILES[$input_file_name]["name"][$i];
            $original_uploaded_data      = $_FILES[$input_file_name]["tmp_name"][$i];

            $upload_result[] = $this->processFile(
                $original_uploaded_file_name,
                $original_uploaded_data,
                '',
                $max_file_size
            );
        }

        // clean up
        $this->cleanUp();

        return $upload_result;
    }


    /**
     * @param string $input_file_name name of <input type="file" size="30" name="upload_file" /> (which is upload_file)
     * @param int $max_file_size max file size in bytes accepts, default = 1MB
     * @param string $fixed_file_name set uploaded name to this fixed name (ie. [item_id].jpg) so old file will be replaced
     * @return FileHandleResponse
     */
    public function handleUpload(string $input_file_name, string $fixed_file_name='', int $max_file_size = 1000000) : FileHandleResponse
    {
        if(!$this->setup_success) {
            return new FileHandleResponse(AppResponse::ERROR, "Fail to setup");
        }

        if(!$input_file_name || !isset($_FILES[$input_file_name])) {
            return new FileHandleResponse('error', 'no file', null);
        }

        $original_uploaded_file_name = $_FILES[$input_file_name]["name"] ?? "";
        $original_uploaded_data      = $_FILES[$input_file_name]["tmp_name"];

        $upload_result = $this->processFile(
            $original_uploaded_file_name,
            $original_uploaded_data,
            $fixed_file_name,
            $max_file_size
        );

        // clean up
        $this->cleanUp();

        return $upload_result;
    }


}
