<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class PageController extends Controller
{
    public function home()
    {
        $trendingProducts = Product::orderBy('num_reviews', 'desc')->take(8)->get();
        $latestProducts = Product::orderBy('created_at', 'desc')->take(5)->get();
        $specialProduct = Product::whereNotNull('old_price')
            ->whereColumn('old_price', '>', 'price')
            ->orderByRaw('(old_price - price) DESC')
            ->first();

        return view('pages.home', compact('trendingProducts', 'latestProducts', 'specialProduct'));
    }

    public function shop(Request $request)
    {
        $query = Product::query();

        if ($request->filled('category')) {
            $query->where('category', $request->category);
        }

        if ($request->filled('search')) {
            $query->where('name', 'like', '%' . $request->search . '%');
        }

        if ($request->filled('brand')) {
            $query->where('brand', $request->brand);
        }

        // Sorting
        switch ($request->get('sort', 'latest')) {
            case 'price_low':
                $query->orderBy('price', 'asc');
                break;
            case 'price_high':
                $query->orderBy('price', 'desc');
                break;
            case 'name':
                $query->orderBy('name', 'asc');
                break;
            default:
                $query->orderBy('created_at', 'desc');
        }

        $products = $query->paginate(9);
        $brands = Product::distinct()->pluck('brand')->sort()->values();

        return view('pages.shop', compact('products', 'brands'));
    }

    public function productDetails($id)
    {
        $product = Product::findOrFail($id);
        $relatedProducts = Product::where('category', $product->category)
            ->where('id', '!=', $product->id)
            ->take(4)
            ->get();

        return view('pages.product-details', compact('product', 'relatedProducts'));
    }

    public function specials()
    {
        $products = Product::whereNotNull('old_price')
            ->whereColumn('old_price', '>', 'price')
            ->orderByRaw('(old_price - price) / old_price DESC')
            ->paginate(8);

        return view('pages.specials', compact('products'));
    }

    public function cart()
    {
        return view('pages.cart');
    }

    public function checkout()
    {
        return view('pages.checkout');
    }

    public function about()
    {
        return view('pages.about');
    }

    public function contact()
    {
        return view('pages.contact');
    }

    public function blog()
    {
        return view('pages.blog');
    }

    public function brands()
    {
        return view('pages.brands');
    }

    public function sitemap()
    {
        return view('pages.sitemap');
    }

    public function dashboard(Request $request)
    {
        $tab = $request->get('tab', 'dashboard');
        return view('pages.dashboard', compact('tab'));
    }
}
