<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use App\Helpers\UploadHelper;
use App\Helpers\UploadUrlHelper;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\FileUploadRequest;
use App\Http\Requests\FileUploadUrlRequest;
use Exception;

class UploadController extends Controller
{
    public function upload(): View
    {
        return view('user.upload.index', [
            'functions' => 'user.upload.function',
            'sidebar' => 'upload',
            'pageName' => pageName([__('lang.upload_file')]),
        ]);
    }

    public function upload_post(
        FileUploadRequest $request
    ): JsonResponse|Response {
        $errors = $request->failedValidation(
            $request->getValidator()
        );
        if ($errors) {
            return response()->json([
                'result' => false,
                'errors' => $errors, 
            ]);
        };

        $formData = $request->only(
            'auto-remove',
            'password',
        );

        foreach ($request->file('file.*') as $file) {

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

                if ($upload[0]) {
                    $slug = pageSlug('file');
                    return response()->json([
                        'result' => true,
                        'url' => url("/{$slug}/{$upload[1]}"),
                    ]);
                }

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

            } catch (Exception $e) {
                Log::error($e);
                return response()->json([
                    'result' => false,
                    'data' => __('lang.error'), 
                ]);
            }
        }

        return response()->json([
            'result' => false,
            'data' => __('lang.error'), 
        ]);
    }

    public function upload_link(
        FileUploadUrlRequest $request
    ): JsonResponse {
        $errors = $request->failedValidation(
            $request->getValidator()
        );
        if ($errors) {
            return response()->json([
                'result' => false,
                'errors' => $errors, 
            ]);
        };
        
        $formData = $request->only(
            'file-url',
            'auto-remove',
            'password',
        );

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

            if ($upload[0]) {
                $slug = pageSlug('file');
                return response()->json([
                    'result' => true,
                    'url' => url("/{$slug}/{$upload[1]}"),
                    'filename' => $upload[2],
                    'filesize' =>$upload[3],
                    'dummy' => url('assets/image/dummy.png'),
                ]);
            }
    
            return response()->json([
                'result' => false,
                'data' => $upload[1], 
            ]);

        } catch (Exception $e) {
            Log::error($e->getMessage());
            return response()->json([
                'result' => false,
                'data' => __('lang.error'), 
            ]);
        }
    }
}
