<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreProductCategoryRequest;
use App\Http\Requests\UpdateProductCategoryRequest;
use App\Models\ProductCategory;
use App\Services\AuditService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProductCategoryController extends Controller
{
    public function index(Request $request): View
    {
        $this->authorize('viewAny', ProductCategory::class);

        $categories = ProductCategory::query()
            ->withCount('products')
            ->when($request->filled('search'), fn ($query) => $query->where('name', 'like', '%'.$request->string('search').'%'))
            ->orderBy('sort_order')
            ->orderBy('name')
            ->paginate(15)
            ->withQueryString();

        return view('product-categories.index', compact('categories'));
    }

    public function create(): View
    {
        $this->authorize('create', ProductCategory::class);

        return view('product-categories.create');
    }

    public function store(StoreProductCategoryRequest $request, AuditService $auditService): RedirectResponse
    {
        $this->authorize('create', ProductCategory::class);

        $category = ProductCategory::query()->create($request->validated());

        $auditService->log('product_category.created', $category, null, $category->toArray());

        return redirect()
            ->route('product-categories.index')
            ->with('success', 'Product category created successfully.');
    }

    public function edit(ProductCategory $productCategory): View
    {
        $this->authorize('update', $productCategory);

        return view('product-categories.edit', ['category' => $productCategory]);
    }

    public function update(UpdateProductCategoryRequest $request, ProductCategory $productCategory, AuditService $auditService): RedirectResponse
    {
        $this->authorize('update', $productCategory);

        $oldValues = $productCategory->toArray();
        $productCategory->update($request->validated());

        $auditService->log('product_category.updated', $productCategory, $oldValues, $productCategory->fresh()->toArray());

        return redirect()
            ->route('product-categories.index')
            ->with('success', 'Product category updated successfully.');
    }

    public function destroy(ProductCategory $productCategory, AuditService $auditService): RedirectResponse
    {
        $this->authorize('delete', $productCategory);

        if ($productCategory->products()->exists()) {
            return back()->with('error', 'Cannot delete a category that has products assigned.');
        }

        $oldValues = $productCategory->toArray();
        $productCategory->delete();

        $auditService->log('product_category.deleted', $productCategory, $oldValues);

        return redirect()
            ->route('product-categories.index')
            ->with('success', 'Product category deleted successfully.');
    }
}
