<?php

namespace App\Http\Controllers\Admin;

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

class HelpFaqController extends Controller
{
    /**
     * Display a listing of FAQs
     */
    public function index()
    {
        $faqs = HelpFaq::orderBy('category')
            ->orderBy('sort_order')
            ->paginate(20);

        $categories = HelpFaq::getCategories();

        return view('admin.help-faqs.index', compact('faqs', 'categories'));
    }

    /**
     * Show form for creating a new FAQ
     */
    public function create()
    {
        $categories = HelpFaq::getCategories();
        return view('admin.help-faqs.form', compact('categories'));
    }

    /**
     * Store a newly created FAQ
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'question' => 'required|string|max:500',
            'answer' => 'required|string',
            'category' => 'required|string|max:50',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'boolean',
        ]);

        $validated['is_active'] = $request->has('is_active');
        $validated['sort_order'] = $validated['sort_order'] ?? 0;

        HelpFaq::create($validated);

        return redirect()->route('admin.help-faqs.index')
            ->with('success', 'FAQ created successfully!');
    }

    /**
     * Show form for editing a FAQ
     */
    public function edit(HelpFaq $helpFaq)
    {
        $categories = HelpFaq::getCategories();
        return view('admin.help-faqs.form', [
            'faq' => $helpFaq,
            'categories' => $categories,
        ]);
    }

    /**
     * Update the specified FAQ
     */
    public function update(Request $request, HelpFaq $helpFaq)
    {
        $validated = $request->validate([
            'question' => 'required|string|max:500',
            'answer' => 'required|string',
            'category' => 'required|string|max:50',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'boolean',
        ]);

        $validated['is_active'] = $request->has('is_active');
        $validated['sort_order'] = $validated['sort_order'] ?? 0;

        $helpFaq->update($validated);

        return redirect()->route('admin.help-faqs.index')
            ->with('success', 'FAQ updated successfully!');
    }

    /**
     * Delete the specified FAQ
     */
    public function destroy(HelpFaq $helpFaq)
    {
        $helpFaq->delete();

        return redirect()->route('admin.help-faqs.index')
            ->with('success', 'FAQ deleted successfully!');
    }

    /**
     * Bulk delete FAQs
     */
    public function bulkDelete(Request $request)
    {
        $ids = $request->input('ids', []);
        
        if (!empty($ids)) {
            HelpFaq::whereIn('id', $ids)->delete();
            return redirect()->route('admin.help-faqs.index')
                ->with('success', count($ids) . ' FAQs deleted successfully!');
        }

        return redirect()->route('admin.help-faqs.index')
            ->with('error', 'No FAQs selected for deletion.');
    }

    /**
     * Toggle FAQ active status
     */
    public function toggleStatus(HelpFaq $helpFaq)
    {
        $helpFaq->update(['is_active' => !$helpFaq->is_active]);

        return redirect()->back()
            ->with('success', 'FAQ status updated!');
    }

    /**
     * Update sort order via AJAX
     */
    public function updateOrder(Request $request)
    {
        $orders = $request->input('orders', []);

        foreach ($orders as $id => $order) {
            HelpFaq::where('id', $id)->update(['sort_order' => $order]);
        }

        return response()->json(['success' => true]);
    }
}
