<?php

namespace App\Http\Controllers\Admin;

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

class CategoryController extends Controller
{

    public function index()
    {
        $categories = Category::latest()->paginate(20);

        $categories->getCollection()->transform(function ($category) {

            $category->image = $category->image
                ? asset('storage/' . $category->image)
                : null;

            return $category;
        });

        return response()->json([
            'status' => true,
            'data' => $categories
        ]);
    }

    public function store(Request $request)
    {

        $request->validate([
            'name' => 'required',
            'image' => 'nullable|image'
        ]);

        $image = null;

        if ($request->hasFile('image')) {

            $image = $request->file('image')
                ->store('categories', 'public');
        }

        $category = Category::create([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'image' => $image,
            'is_active' => $request->is_active ?? true,
            'sort_order' => $request->sort_order ?? 0
        ]);

        return response()->json([
            'status' => true,
            'message' => 'Category created successfully',
            'data' => $category
        ]);
    }

    public function show($id)
    {
        $category = Category::findOrFail($id);

        return response()->json([
            'status' => true,
            'data' => $category
        ]);
    }

    public function update(Request $request, $id)
    {
        $category = Category::findOrFail($id);

        $request->validate([
            'name' => 'required'
        ]);

        $image = $category->image;

        if ($request->hasFile('image')) {

            $image = $request->file('image')
                ->store('categories', 'public');
        }

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

        return response()->json([
            'status' => true,
            'message' => 'Category updated successfully'
        ]);
    }

    public function destroy($id)
    {
        $category = Category::findOrFail($id);

        $category->delete();

        return response()->json([
            'status' => true,
            'message' => 'Category deleted successfully'
        ]);
    }

    // ========================= MOBILE APP APIS ==============================

   
public function list(Request $request)
{
    $limit = $request->limit;

    $query = Category::withCount('products')
        ->where('is_active', 1)
        ->orderBy('sort_order');

    if ($limit && $limit !== 'all') {
        $categories = $query->limit((int)$limit)->get();
    } else {
        $categories = $query->get();
    }

    $defaultImage = asset('mastya.png');

    $data = $categories->map(function ($category) use ($defaultImage) {

        $image = $category->image
            ? asset('storage/' . $category->image)
            : $defaultImage;

        return [
            'id' => $category->id,
            'name' => $category->name,
            'slug' => $category->slug,
            'image' => $image,
            'product_count' => $category->products_count,
        ];
    });

    return response()->json([
        'success' => true,
        'message' => 'Categories fetched successfully',
        'count' => $data->count(),
        'data' => $data
    ]);
}
}
