<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Models\Storages;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class SettingsStorageController extends Controller
{
    public function storage(): View
    {
        $defaultStorage = storageSetting(1);
        $amazonS3 = storageSetting(2);
        $cloudflareR2 = storageSetting(3);
        $wasabi = storageSetting(4);
        $ftp = storageSetting(5);
        $google = storageSetting(6);

        return view('admin.settings.storage.index', [
            'functions' => 'admin.settings.storage.function',
            'sidebar' => 'storage_settings',
            'pageName' => pageName([__('lang.settings'), __('lang.storage')]),
            'defaultStorage' => $defaultStorage,
            'amazonS3' => $amazonS3,
            'cloudflareR2' => $cloudflareR2,
            'wasabi' => $wasabi,
            'ftp' => $ftp,
            'google' => $google,
        ]);
    }

    public function storage_post(
        Request $request
    ): RedirectResponse {
        $storageData = Storages::get();
        $storageIds = [];
        foreach ($storageData as $value) {
            array_push($storageIds, $value->id);
        }

        $defaultStorageId = $request->input('default-storage');
        if (!in_array($defaultStorageId, $storageIds)) {
            return back()
                ->with('error', __('lang.data_update_error'));
        }

        foreach ($storageData as $value) {
            Storages::where('id', $value->id)
                ->update([
                    'default' => null,
                ]);
        }

        $update = Storages::where('id', $defaultStorageId)
            ->update([
                'default' => true,
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }

    public function storage_s3_post(
        Request $request
    ): RedirectResponse {
        $request->validate([
            'amazon-key' => 'required|string|max:100',
            'amazon-secret' => 'required|string|max:100',
            'amazon-region' => 'required|string|max:100',
            'amazon-bucket' => 'required|string|max:100',
            'amazon-url' => 'required|url|max:100',
        ]);

        $userId = Auth::id();
        $userIp = $request->ip();
        $value = json_encode([
            "access_key_id" => $request->input('amazon-key'),
            "secret_access_key" => $request->input('amazon-secret'),
            "default_region" => $request->input('amazon-region'),
            "bucket" => $request->input('amazon-bucket'),
            "url" => $request->input('amazon-url'),
        ]);

        $update = Storages::where('id', 2)
            ->update([
                'updated_by_id' => $userId,
                'updated_by_ip' => $userIp,
                'value' => $value
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }

    public function storage_r2_post(
        Request $request
    ): RedirectResponse {
        $request->validate([
            'cloudflare-key' => 'required|string|max:100',
            'cloudflare-secret' => 'required|string|max:100',
            'cloudflare-bucket' => 'required|string|max:100',
            'cloudflare-url' => 'required|url|max:100',
            'cloudflare-endpoint' => 'required|url|max:100',
        ]);

        $userId = Auth::id();
        $userIp = $request->ip();
        $value = json_encode([
            "access_key_id" => $request->input('cloudflare-key'),
            "secret_access_key" => $request->input('cloudflare-secret'),
            "bucket" => $request->input('cloudflare-bucket'),
            "url" => $request->input('cloudflare-url'),
            "endpoint" => $request->input('cloudflare-endpoint'),
        ]);

        $update = Storages::where('id', 3)
            ->update([
                'updated_by_id' => $userId,
                'updated_by_ip' => $userIp,
                'value' => $value
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }

    public function storage_wasabi_post(
        Request $request
    ): RedirectResponse {
        $request->validate([
            'wasabi-key' => 'required|string|max:100',
            'wasabi-secret' => 'required|string|max:100',
            'wasabi-region' => 'required|string|max:100',
            'wasabi-bucket' => 'required|string|max:100',
            'wasabi-url' => 'required|url|max:100',
        ]);

        $userId = Auth::id();
        $userIp = $request->ip();
        $value = json_encode([
            "access_key_id" => $request->input('wasabi-key'),
            "secret_access_key" => $request->input('wasabi-secret'),
            "default_region" => $request->input('wasabi-region'),
            "bucket" => $request->input('wasabi-bucket'),
            "url" => $request->input('wasabi-url'),
        ]);

        $update = Storages::where('id', 4)
            ->update([
                'updated_by_id' => $userId,
                'updated_by_ip' => $userIp,
                'value' => $value
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }

    public function storage_ftp_post(
        Request $request
    ): RedirectResponse {
        $request->validate([
            'ftp-host' => 'required|string|max:100',
            'ftp-username' => 'required|string|max:100',
            'ftp-password' => 'required|string|max:100',
            'ftp-port' => 'required|numeric',
            'ftp-path' => 'required|string|max:100',
            'ftp-url' => 'required|string|max:100',
        ]);

        $userId = Auth::id();
        $userIp = $request->ip();
        $value = json_encode([
            "host" => $request->input('ftp-host'),
            "username" => $request->input('ftp-username'),
            "password" => $request->input('ftp-password'),
            "port" => $request->input('ftp-port'),
            "path" => $request->input('ftp-path'),
            "url" => $request->input('ftp-url'),
        ], JSON_UNESCAPED_SLASHES);

        $update = Storages::where('id', 5)
            ->update([
                'updated_by_id' => $userId,
                'updated_by_ip' => $userIp,
                'value' => $value
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }

    public function storage_google_post(
        Request $request
    ): RedirectResponse {
        $request->validate([
            'google-client-id' => 'required|string|max:500',
            'google-client-secret' => 'required|string|max:500',
            'google-refresh-token' => 'required|string|max:500',
            'google-folder' => 'nullable|string|max:100',
        ]);

        $userId = Auth::id();
        $userIp = $request->ip();
        $value = json_encode([
            "client_id" => $request->input('google-client-id'),
            "client_secret" => $request->input('google-client-secret'),
            "refresh_token" => $request->input('google-refresh-token'),
            "folder" => $request->input('google-folder'),
        ]);

        $update = Storages::where('id', 6)
            ->update([
                'updated_by_id' => $userId,
                'updated_by_ip' => $userIp,
                'value' => $value
            ]);

        if ($update) {

            if (Cache::has('storageSetting')) {
                Cache::forget('storageSetting');
            }

            return back()
                ->with('success', __('lang.data_update'));
        }

        return back()
            ->with('error', __('lang.data_update_error'));
    }
    
    public function temp_files(): RedirectResponse {
        $tempFiles = Storage::disk('temp')->allFiles();

        foreach ($tempFiles as $file) {
            Storage::disk('temp')->delete($file);
        }

        return back()
            ->with('success', __('lang.data_delete'));
    }

}
