<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AppConfig;
use Illuminate\Support\Facades\Storage;

class AppConfigController extends Controller
{
    public function index()
    {
        $logo = AppConfig::first();
        if (!$logo) {
            return sendSuccessResponse([]);
        }
        $logo_default = null;
        $logo_light = null;
        $logo_dark = null;
        if ($logo->logo_default || $logo->logo_default != '') {
            $logo_default = asset('logos/' . $logo->logo_default);
        }
        if ($logo->logo_light || $logo->logo_light != '') {
            $logo_light = asset('logos/' . $logo->logo_light);
        }
        if ($logo->logo_dark || $logo->logo_dark != '') {
            $logo_dark = asset('logos/' . $logo->logo_dark);
        }
        return sendSuccessResponse([
            'id' => $logo->id,
            'title' => $logo->title,
            'logo_default' => $logo_default,
            'logo_light' => $logo_light,
            'logo_dark' => $logo_dark
        ]);
    }
    public function logoManagement()
    {
        $logo = AppConfig::first();
        return view('Backend.LogoManagement.logo-management', ['logo' => $logo]);
    }
    public function updateLogo(Request $request)
    {
        try {
            $request->validate([
                'logo_default' => 'required_without:keep_default|image|mimes:jpeg,png,jpg,gif,webp|max:10240',
                'logo_light' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:10240',
                'title' => 'required|string|max:255'
            ]);

            $logo = AppConfig::firstOrNew();
            $updateData = ['title' => $request->title];

            // Handle default logo
            if ($request->hasFile('logo_default')) {
                $updateData['logo_default'] = uploadImageLocal($request->file('logo_default'), 'logo_default', 'logos');
                $updateData['logo_dark'] = uploadImageLocal($request->file('logo_default'), 'logo_dark', 'logos');
            }

            // Handle light logo
            if ($request->hasFile('logo_light')) {
                $updateData['logo_light'] = uploadImageLocal($request->file('logo_light'), 'logo_light', 'logos');
            } elseif ($request->has('remove_light_logo')) {
                deleteOldFileR2('logos', $logo->logo_light);
                $updateData['logo_light'] = null;
            }

            $logo->exists ? $logo->update($updateData) : $logo->fill($updateData)->save();

            return redirect()->back()
                ->with('success', 'Logo configuration updated successfully')
                ->with('toast', true);
        } catch (\Exception $e) {
            dd($e);
            return redirect()->back()
                ->with('error', 'Error updating logo configuration: ' . $e->getMessage())
                ->with('toast', true);
        }
    }

    public function updateDefaultLogo(Request $request)
    {
        try {
            $request->validate([
                'logo_default' => 'required|image|mimes:jpeg,png,jpg,gif,webp,svg|max:10240',
                'title' => 'required|string|max:255'
            ]);
            $logo = AppConfig::firstOrNew();
            if ($request->hasFile('logo_default')) {
                if (isset($logo->logo_default)) {
                    deleteFileLocal('logos', $logo->logo_default);
                }

                $file = $request->file('logo_default');
                $uploadResult = uploadImageLocal($file, 'logo_default', 'logos');
                $updateData = [
                    'logo_default' => $uploadResult,
                    'logo_dark' => $uploadResult,
                    'title' => $request->title
                ];
                $logo->exists ? $logo->update($updateData) : $logo->fill($updateData)->save();
            }

            toastr()->success('Default logo updated successfully');
            return back();
        } catch (\Exception $e) {
            toastr()->error('Error updating default logo: ' . $e->getMessage());
            return back();
        }
    }

    public function updateLightLogo(Request $request)
    {
        try {
            $request->validate([
                'logo_light' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp|max:10240',
                'title' => 'required|string|max:255'
            ]);

            $logo = AppConfig::firstOrNew();

            if ($request->hasFile('logo_light')) {
                if (isset($logo->logo_light)) {
                    deleteFileLocal('logos', $logo->logo_light);
                }
                $uploadResult = uploadImageLocal($request->logo_light, 'logo_light', 'logos');

                $updateData = ['logo_light' => $uploadResult, 'title' => $request->title];

                $logo->exists ? $logo->update($updateData) : $logo->fill($updateData)->save();
            }

            toastr()->success('Light logo updated successfully');
            return back();
        } catch (\Exception $e) {
            toastr()->error('Error updating light logo: ' . $e->getMessage());
            return back();
        }
    }

    public function removeLightLogo()
    {
        try {
            $logo = AppConfig::first();
            if ($logo) {
                deleteOldFileR2('speed-net/logos', $logo->logo_light);
                $logo->update(['logo_light' => null]);
            }

            toastr()->success('Light logo removed successfully');
            return back();
        } catch (\Exception $e) {
            toastr()->error('Error removing light logo: ' . $e->getMessage());
            return back();
        }
    }
}
