<?php

namespace App\Http\Controllers\Rest;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use App\Helpers\UploadRestHelper;
use App\Helpers\UploadRestUrlHelper;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\FileUploadRestRequest;
use App\Http\Requests\FileUploadRestUrlRequest;
use Exception;

class UploadController extends Controller
{
    public function upload(
        FileUploadRestRequest $request
    ): JsonResponse|Response {
        $errors = $request->failedValidation(
            $request->getValidator()
        );
        if ($errors) {
            return response()->json([
                'result' => false,
                'errors' => $errors, 
            ], 400);
        };

        $formData = $request->only(
            'type',
            'compress',
            'size.width',
            'size.height',
            'rotation-reflect',
            'watermark',
            'preferences.brightness',
            'preferences.contrast',
            'preferences.gamma',
            'preferences.red',
            'preferences.green',
            'preferences.blue',
            'effects.grayscale',
            'effects.blur',
            'effects.invert',
            'effects.pixelation',
            'effects.reduce',
            'auto-remove',
            'password',
            'exif.basic',
            'exif.deep',
        );

        if (!$request->hasFile('file')) {
            return response()->json([
                'result' => false,
                'data' => 'The file to upload could not be found.', 
            ], 400);
        }

        $file = $request->file('file');

        try {
            $uploader = new UploadRestHelper();
            $upload = $uploader->upload(
                $file,
                $formData
            );

            if ($upload[0]) {
                return response()->json([
                    'result' => true,
                    'url' => $upload[1], 
                    'thumb' => $upload[2],
                ], 200);
            }

            return response()->json([
                'result' => false,
                'data' => $upload[1], 
            ], 400);

        } catch (Exception $e) {
            Log::error($e);
            return response()->json([
                'result' => false,
                'data' => 'An unknown error occurred, try again later.', 
            ], 500);
        }

    }

    public function upload_link(
        FileUploadRestUrlRequest $request
    ): JsonResponse {
        $errors = $request->failedValidation(
            $request->getValidator()
        );
        if ($errors) {
            return response()->json([
                'result' => false,
                'errors' => $errors, 
            ], 400);
        };
        
        $formData = $request->only(
            'file-url',
            'type',
            'compress',
            'size.width',
            'size.height',
            'rotation-reflect',
            'watermark',
            'preferences.brightness',
            'preferences.contrast',
            'preferences.gamma',
            'preferences.red',
            'preferences.green',
            'preferences.blue',
            'effects.grayscale',
            'effects.blur',
            'effects.invert',
            'effects.pixelation',
            'effects.reduce',
            'auto-remove',
            'password',
            'exif',
            'exif.deep',
        );

        try {
            $uploadUrlHelper = new UploadRestUrlHelper();
            $fileUrl = $request->input('file-link');
            $upload = $uploadUrlHelper->upload(
                $fileUrl,
                $formData
            );

            if ($upload[0]) {
                return response()->json([
                    'result' => true,
                    'url' => $upload[1], 
                    'thumb' => $upload[2],
                ], 200);
            }
    
            return response()->json([
                'result' => false,
                'data' => $upload[1], 
            ], 400);

        } catch (Exception $e) {
            Log::error($e->getMessage());
            return response()->json([
                'result' => false,
                'data' => 'An unknown error occurred, try again later.', 
            ], 500);
        }
    }
}
