<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Upload;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use STS\ZipStream\Facades\Zip;
use Illuminate\Support\Facades\Log;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class DownloadController extends Controller
{
    public function get_zip(
        Request $request
    ): mixed {
        try{
            sleep(1);
            $fileKeys = $request->input('keys');
            $fileKeysArr = explode(',',$fileKeys);

            if (count($fileKeysArr) > 0) {

                $zipKey = $this->randomKey();
                $zip = Zip::create("{$zipKey}.zip");

                foreach ($fileKeysArr AS $fileKey) {
                    $file = Upload::whereRaw("BINARY `short_key`= ?", $fileKey)
                        ->first();
                    if ($file) {
                        $zip->addFromDisk(
                            $file->disk, 
                            $file->filename,
                            "{$file->short_key}.{$file->filetype}"
                        );
                    }
                }

                if ($zip->saveToDisk("temp", "/")) {
                    $downloadUrl = LaravelLocalization::localizeUrl(
                        "/download-zip/{$zipKey}"
                    );
                    return response()->json([
                        'result' => true,
                        'data' => $downloadUrl,
                    ]);
                } else {
                    return response()->json([
                        'result' => false,
                        'data' => __('lang.error'),
                    ]);
                }
            }

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

    }

    public function download_zip(string $zipKey): mixed {
        try{
            return Storage::disk('temp')->download("{$zipKey}.zip");
        } catch (\Exception $e) {
            Log::error($e);
            return [
                false,
                __('lang.error'),
            ];
        }
    }
    
    public function get_file(
        Request $request
    ): mixed {
        try{
            sleep(1);
            $fileKey = $request->input('keys');

            if ($fileKey) {
                $file = Upload::whereRaw("BINARY `short_key`= ?", $fileKey)
                    ->first();

                if ($file) {
                    $downloadUrl = LaravelLocalization::localizeUrl(
                        "/download-single/{$file->short_key}"
                    );
                    return response()->json([
                        'result' => true,
                        'data' => $downloadUrl,
                    ]);
                } else {
                    return response()->json([
                        'result' => false,
                        'data' => __('lang.file_not_found'),
                    ]);
                }
            }

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

    }

    public function download_file(string $fileKey): mixed {
        try{
            $file = Upload::whereRaw("BINARY `short_key`= ?", $fileKey)
                ->first();
            $filename = "{$file->short_key}.{$file->filetype}";
            return Storage::disk($file->disk)
                ->download($file->filename,$filename);
        } catch (\Exception $e) {
            Log::error($e);
            return [
                false,
                __('lang.error'),
            ];
        }
    }
    
    private function randomKey(): mixed {
        return Str::random(16);
    }

}
