<?php

namespace Livewire\Features\SupportFileUploads;

use Illuminate\Support\Facades\Validator;

class FileUploadController
{
    public function getMiddleware()
    {
        /**
         * Laravel requires the returned array to contain an array for each
         * middleware with `middleware` and `options` keys. So we'll map
         * through the file upload config middleware and format them.
         */
        return array_map(
            fn($middleware) => [
                'middleware' => $middleware,
                'options' => [],
            ],
            (array) FileUploadConfiguration::middleware()
        );
    }

    public function handle()
    {
        abort_unless(request()->hasValidSignature(), 401);

        $disk = FileUploadConfiguration::disk();

        $filePaths = $this->validateAndStore(request('files'), $disk);

        return ['paths' => $filePaths];
    }

    public function validateAndStore($files, $disk)
    {
        Validator::make(['files' => $files], [
            'files.*' => FileUploadConfiguration::rules()
        ])->validate();

        $fileHashPaths = collect($files)->map(function ($file) use ($disk) {
            $filename = TemporaryUploadedFile::generateHashNameWithOriginalNameEmbedded($file);

            return $file->storeAs('/'.FileUploadConfiguration::path(), $filename, [
                'disk' => $disk
            ]);
        });

        // Strip out the temporary upload directory from the paths.
        return $fileHashPaths->map(function ($path) { return str_replace(FileUploadConfiguration::path('/'), '', $path); });
    }
}
