<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SidebarSetting;
use Illuminate\Http\Request;

class SidebarController extends Controller
{
    /**
     * Display all sidebar settings.
     */
    public function index()
    {
        $settings = SidebarSetting::orderBy('sort_order')->get();
        $modules = SidebarSetting::getModules();

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

    /**
     * Show the form for editing a sidebar setting.
     */
    public function edit(SidebarSetting $sidebar)
    {
        $modules = SidebarSetting::getModules();

        return view('admin.sidebars.edit', compact('sidebar', 'modules'));
    }

    /**
     * Update the sidebar setting.
     */
    public function update(Request $request, SidebarSetting $sidebar)
    {
        $validated = $request->validate([
            'label'            => 'required|string|max:100',
            'title'            => 'nullable|string|max:150',
            'is_enabled'       => 'boolean',
            'show_search'      => 'boolean',
            'show_categories'  => 'boolean',
            'show_user_widget' => 'boolean',
            'show_popular'     => 'boolean',
            'custom_html'      => 'nullable|string',
            'sort_order'       => 'integer|min:0',
        ]);

        $validated['is_enabled']       = $request->boolean('is_enabled', true);
        $validated['show_search']      = $request->boolean('show_search', true);
        $validated['show_categories']  = $request->boolean('show_categories', true);
        $validated['show_user_widget'] = $request->boolean('show_user_widget', true);
        $validated['show_popular']     = $request->boolean('show_popular', false);

        $sidebar->update($validated);
        SidebarSetting::clearCache();

        return redirect()->route('admin.sidebars.index')
            ->with('success', 'Sidebar ayarları güncellendi.');
    }

    /**
     * Reset a sidebar setting to defaults.
     */
    public function reset(SidebarSetting $sidebar)
    {
        $defaults = $this->getDefaultSettings($sidebar->module);
        $sidebar->update($defaults);
        SidebarSetting::clearCache();

        return redirect()->route('admin.sidebars.index')
            ->with('success', 'Sidebar ayarları varsayılanlara sıfırlandı.');
    }

    /**
     * Get default settings for a module.
     */
    protected function getDefaultSettings(string $module): array
    {
        return [
            'module'            => $module,
            'label'             => SidebarSetting::getModules()[$module] ?? $module,
            'title'             => null,
            'is_enabled'        => true,
            'show_search'       => true,
            'show_categories'   => true,
            'show_user_widget'  => true,
            'show_popular'      => false,
            'custom_html'       => null,
            'sort_order'        => 0,
        ];
    }
}
