<?php

namespace App\Helpers;

use App\Models\Upload;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Helpers\PlanHelper;
use App\Helpers\NudityHelper;
use Illuminate\Support\Facades\Log;

class UploadRestHelper
{
    private static $defaultStorage;

    public function __construct() 
    {
        self::$defaultStorage = defaultStorage()->storage_key;
    }

    public static function upload(
        object $file, 
        array $formData
    ): array {
        $request = request();
        $token = $request->header('token');
        
        if (!$token) {
            return [
                false,
                'User token is missing.'
            ];
        }

        $userData = DB::table('users')
            ->where('api_token', $token)
            ->first();

        if (!$userData) {
            return [
                false,
                'User token is wrong.'
            ];
        }

        $canIRest = PlanHelper::canIRest(
            $userData->id, 
        );

        if (!$canIRest[0]) {
            return [
                false,
                $canIRest[1], 
            ];
        }

        $canIUpload = PlanHelper::canIUpload(
            $userData->id, 
            $file, 
            $file->getSize()
        );

        if (!$canIUpload[0]) {
            return [
                false,
                $canIUpload[1], 
            ];
        }

        $fileRandom = Str::random(40);
        $fileType = $file->getClientOriginalExtension();
        $fileName = "{$fileRandom}.{$fileType}";

        $upload = Storage::disk('temp')
            ->putFileAs(
                '/', 
                $file, 
                $fileName, 
                'private'
            );

        if (!$upload) {
            return [
                false,
                'The file could not be uploaded.', 
            ];
        }

        $nudityChecker = setting('nudity_status');
        if ($nudityChecker == 1) {
            $nudityCheck = NudityHelper::check($fileName);
            if (!$nudityCheck[0]) {
                return [
                    false,
                    $nudityCheck[1], 
                ];
            }
        }

        $manipulate = self::start(
            $fileName,
            $formData,
            $userData->id
        );

        if ($manipulate[0]) {
            return [
                true,
                url("get/{$manipulate[1]}"),
                url("file/{$manipulate[1]}"),
            ];
        } else {
            Storage::disk('temp')
                ->delete($fileName);

            return [
                false,
                $manipulate[1], 
            ];
        }
    }

    public static function start(
        string $fileName, 
        array $formData,
        int $userId
    ): array {
        $database = self::database(
            $fileName,
            $formData,
            $userId,
        );

        if ($database[0]) {
            return [
                true,
                $database[1],
            ];
        } else {
            return [
                false,
                $database[1],
            ];
        }
    }

    public static function database(
        string $fileName, 
        array $formData,
        int $userId,
    ): array {
        $request = request();
        
        if (!Storage::disk('temp')->exists($fileName)) {
            return [
                false, 
                'File not found.'
            ];
        }

        $userIp = $request->ip();
        $fileNameArr = explode('.', $fileName);
        $fileSize = Storage::disk('temp')
            ->size($fileName);

        $fileData['created_by_id'] = $userId;
        $fileData['created_by_ip'] = $userIp;
        $fileData['updated_by_id'] = $userId;
        $fileData['updated_by_ip'] = $userIp;
        $fileData['filename'] = $fileName;
        $fileData['filesize'] = $fileSize;
        $fileData['filetype'] = $fileNameArr[1];
        $fileData['disk'] = self::$defaultStorage;

        if (isset($formData['auto-remove']) && $formData['auto-remove']) {
            $fileData['autoremove'] = $formData['auto-remove'];
        }

        if (isset($formData['password']) && $formData['password']) {
            $fileData['password'] = $formData['password'];
        }
        
        $create = Upload::create($fileData);

        if ($create) {

            try{
                Storage::disk(self::$defaultStorage)->put(
                    $fileName,
                    Storage::disk('temp')->get($fileName)
                );

                Storage::disk('temp')->delete($fileName);

                return [
                    true, 
                    $fileName,
                ];
            } catch (\Exception $e) {
                Log::error($e);
                Upload::where('id', $create->id)->delete();
                return [
                    false,
                    'The file could not be uploaded.',
                ];
            }
        }

        return [
            false, 
            'The file could not be uploaded.',
        ];
    }
    
}
