<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\Category;
use App\Models\DeviceInterface;
use App\Models\Product;
use Illuminate\Http\Request;

class ShopController extends Controller
{
    public function index()
    {
        $products = Product::active()->with('category', 'primaryImage')->paginate(12);
        $categories = Category::active()->parents()->with('children')->ordered()->get();
        return view('front.shop', compact('products', 'categories'));
    }

    public function category($slug)
    {
        $category = Category::where('slug', $slug)->firstOrFail();
        $products = Product::active()->where('category_id', $category->id)
            ->with('category', 'primaryImage')->paginate(12);
        $categories = Category::active()->parents()->with('children')->ordered()->get();
        return view('front.shop', compact('products', 'categories', 'category'));
    }

    public function show($slug)
    {
        $product = Product::where('slug', $slug)->active()->with('category', 'images')->firstOrFail();
        $relatedProducts = Product::active()->where('category_id', $product->category_id)
            ->where('id', '!=', $product->id)->with('primaryImage')->take(4)->get();
        return view('front.product', compact('product', 'relatedProducts'));
    }

    public function brand($slug)
    {
        $brand = Brand::where('slug', $slug)->active()->firstOrFail();
        $products = Product::active()->with('category', 'primaryImage')
            ->where('name', 'like', "%{$brand->name}%")
            ->orWhere('description', 'like', "%{$brand->name}%")
            ->paginate(12);
        $categories = Category::active()->parents()->with('children')->ordered()->get();
        return view('front.shop', compact('products', 'categories', 'brand'));
    }

    public function interface($slug)
    {
        $interface = DeviceInterface::where('slug', $slug)->firstOrFail();
        $products = Product::active()->with('category', 'primaryImage')
            ->where(function ($q) use ($interface) {
                $q->where('name', 'like', "%{$interface->name}%")
                  ->orWhere('description', 'like', "%{$interface->name}%");
            })->paginate(12);
        $categories = Category::active()->parents()->with('children')->ordered()->get();
        return view('front.shop', compact('products', 'categories', 'interface'));
    }

    public function search(Request $request)
    {
        $query = $request->get('q');
        $products = Product::active()->where(function ($q) use ($query) {
            $q->where('name', 'like', "%{$query}%")
              ->orWhere('description', 'like', "%{$query}%")
              ->orWhere('short_description', 'like', "%{$query}%")
              ->orWhere('sku', 'like', "%{$query}%");
        })->with('category', 'primaryImage')->paginate(12);

        $categories = Category::active()->parents()->with('children')->ordered()->get();
        return view('front.shop', compact('products', 'categories', 'query'));
    }
}
