<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CategoryController extends Controller
{
    // 1. صفحة عرض كل الأقسام
    public function index()
    {
        $categories = Category::latest()->get();
        return view('admin.categories.index', compact('categories'));
    }

    // 2. صفحة فورم الإضافة
    public function create()
    {
        return view('admin.categories.create');
    }

    // 3. حفظ القسم الجديد في الداتابيز
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255|unique:categories,name',
        ]);

        Category::create([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'is_active' => $request->has('is_active') // لو متعلم عليه هيبقى 1، لو لأ هيبقى 0
        ]);

        return redirect()->route('admin.categories.index')->with('success', 'Category created successfully!');
    }

    // 4. صفحة فورم التعديل
    public function edit($locale, $id)
    {
        $category = Category::findOrFail($id);
        return view('admin.categories.edit', compact('category'));
    }

    // 5. تحديث البيانات في الداتابيز
    public function update($locale, Request $request, $id)
    {
        $category = Category::findOrFail($id);

        $request->validate([
            'name' => 'required|string|max:255|unique:categories,name,' . $id,
        ]);

        $category->update([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'is_active' => $request->has('is_active')
        ]);

        return redirect()->route('admin.categories.index')->with('success', 'Category updated successfully!');
    }

    // 6. مسح القسم تماماً
    public function destroy($locale, $id)
    {
        $category = Category::findOrFail($id);
        $category->delete();

        return redirect()->route('admin.categories.index')->with('success', 'Category deleted successfully!');
    }


public function bulkAction(Request $request)
{
    $ids = $request->input('ids', []);
    $action = $request->input('action');

    if (empty($ids)) {
        return response()->json(['success' => false, 'message' => 'No categories selected.']);
    }

    // 1. الحذف الجماعي
    if ($action === 'delete') {
        Category::whereIn('id', $ids)->delete();
        session()->flash('success', 'Selected categories deleted successfully!');
        return response()->json(['success' => true]);
    }

    // 2. التفعيل الجماعي (Active) 🟢
    if ($action === 'activate') {
        Category::whereIn('id', $ids)->update(['is_active' => 1]); // حطينا 1 صريحة للداتابيز
        session()->flash('success', 'Selected categories activated successfully!');
        return response()->json(['success' => true]);
    }

    // 3. التعطيل الجماعي (Inactive) 🔴
    if ($action === 'deactivate') {
        Category::whereIn('id', $ids)->update(['is_active' => 0]); // حطينا 0 صريحة للداتابيز
        session()->flash('success', 'Selected categories deactivated successfully!');
        return response()->json(['success' => true]);
    }

    return response()->json(['success' => false, 'message' => 'Invalid action type.']);
}

	
}