<?php

namespace App\Http\Controllers\Admin;

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

class SettingController extends Controller
{
    /**
     * Display the settings page.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $settings = [
            'site_name' => Setting::getValue('site_name', config('app.name')),
            'site_description' => Setting::getValue('site_description', ''),
            'site_logo' => Setting::getValue('site_logo', null),
            'disable_registration' => Setting::getValue('disable_registration', false),
        ];

        return view('admin.settings.index', compact('settings'));
    }

    /**
     * Update the settings.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
        $request->validate([
            'site_name' => 'required|string|max:255',
            'site_description' => 'nullable|string|max:500',
            'site_logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'disable_registration' => 'nullable|boolean',
        ]);

        // Update site name
        Setting::setValue('site_name', $request->input('site_name'));

        // Update site description
        Setting::setValue('site_description', $request->input('site_description', ''));

        // Update disable registration
        Setting::setValue('disable_registration', $request->has('disable_registration') ? true : false);

        // Handle logo upload
        if ($request->hasFile('site_logo')) {
            // Delete old logo if exists
            $oldLogo = Setting::getValue('site_logo');
            if ($oldLogo) {
                Storage::disk('public')->delete($oldLogo);
            }

            // Store new logo
            $path = $request->file('site_logo')->store('settings', 'public');
            Setting::setValue('site_logo', $path);
        }

        ActivityLogger::log('settings.updated', __('Main settings updated'));

        return redirect()->route('admin.settings.index')
            ->with('success', __('Settings updated successfully!'));
    }

    /**
     * Display the general settings page.
     *
     * @return \Illuminate\View\View
     */
    public function general()
    {
        $settings = [
            'site_name' => Setting::getValue('site_name', config('app.name')),
            'site_url' => Setting::getValue('site_url', url('/')),
            'site_description' => Setting::getValue('site_description', ''),
            'site_logo' => Setting::getValue('site_logo', null),
            'disable_registration' => (bool) Setting::getValue('disable_registration', false),
        ];

        return view('admin.settings.general', compact('settings'));
    }

    /**
     * Update the general settings.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function updateGeneral(Request $request)
    {
        $request->validate([
            'site_name' => 'required|string|max:255',
            'site_url' => 'required|url|max:255',
            'site_description' => 'nullable|string|max:500',
        ]);

        // Update site name
        Setting::setValue('site_name', $request->input('site_name'));

        // Update site URL
        Setting::setValue('site_url', $request->input('site_url'));

        // Update site description
        Setting::setValue('site_description', $request->input('site_description', ''));

        ActivityLogger::log('settings.general_updated', __('General settings updated'));

        return redirect()->route('admin.settings.general')
            ->with('success', __('Settings updated successfully!'));
    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function logo()
    {
        return redirect()->to(route('admin.settings.general').'#settings-logo');
    }

    /**
     * Update the logo settings.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function updateLogo(Request $request)
    {
        $request->validate([
            'site_logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        // Handle logo upload
        if ($request->hasFile('site_logo')) {
            // Delete old logo if exists
            $oldLogo = Setting::getValue('site_logo');
            if ($oldLogo) {
                Storage::disk('public')->delete($oldLogo);
            }

            // Store new logo
            $path = $request->file('site_logo')->store('settings', 'public');
            Setting::setValue('site_logo', $path);

            ActivityLogger::log('settings.logo_updated', __('Logo updated'));
        }

        return redirect()->to(route('admin.settings.general').'#settings-logo')
            ->with('success', __('Settings updated successfully!'));
    }

    /**
     * Display the registration settings page.
     *
     * @return \Illuminate\View\View
     */
    public function registration()
    {
        return redirect()->to(route('admin.settings.general').'#settings-registration');
    }

    /**
     * Update the registration settings.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function updateRegistration(Request $request)
    {
        $request->validate([
            'disable_registration' => 'nullable|boolean',
        ]);

        // Update disable registration
        Setting::setValue('disable_registration', $request->has('disable_registration') ? true : false);

        ActivityLogger::log('settings.registration_updated', __('Registration settings updated'));

        return redirect()->to(route('admin.settings.general').'#settings-registration')
            ->with('success', __('Settings updated successfully!'));
    }
}
