<?php
namespace App\Http\Controllers\website;

use App;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\ProductCat;
use App\Traits\FileUploadTrait;
use App\WebRepositories\Interfaces\EnqueryInterface;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    use FileUploadTrait;

    private $repoEnquery;

    public function __construct(EnqueryInterface $repoEnquery)
    {

        $this->repoEnquery = $repoEnquery;

    }

    public function index(Request $request)
    {
        return view('website.home');
    }
    public function aboutUs(Request $request)
    {
        return view('website.about');
    }
    public function contactUs(Request $request)
    {
        return view('website.contact');
    }

    public function category($category_slug)
    {
        $category = ProductCat::where('slug', $category_slug)
            ->where('is_delete', 0)
            ->where('status', 1)
            ->firstOrFail();

        $baseImgPath = $this->getBasePath();

        $products = Product::where('cat_id', $category->id)
            ->where('is_delete', 0)
            ->where('status', 1)
            ->orderBy('created_at', 'DESC')
            ->paginate(12);

        // Add full image paths to each product
        $products->getCollection()->transform(function ($product) use ($baseImgPath) {
            $product->brand_logo = $product->brand_logo ? $baseImgPath . $product->brand_logo : null;
            $product->img_1      = $product->img_1 ? $baseImgPath . $product->img_1 : null;
            $product->img_2      = $product->img_2 ? $baseImgPath . $product->img_2 : null;
            $product->img_3      = $product->img_3 ? $baseImgPath . $product->img_3 : null;
            $product->img_4      = $product->img_4 ? $baseImgPath . $product->img_4 : null;
            return $product;
        });

        return view('website.products.category', compact('category', 'products'));
    }

    public function show($category_slug, $product_slug)
    {
        $product = Product::with('category')
            ->where('slug', $product_slug)
            ->where('is_delete', 0)
            ->where('status', 1)
            ->firstOrFail();

        // Verify the product belongs to the category
        if ($product->category->slug !== $category_slug) {
            abort(404);
        }

        $baseImgPath = $this->getBasePath();

        // Add full image paths to the product
        $product->brand_logo = $product->brand_logo ? $baseImgPath . $product->brand_logo : null;
        $product->img_1      = $product->img_1 ? $baseImgPath . $product->img_1 : null;
        $product->img_2      = $product->img_2 ? $baseImgPath . $product->img_2 : null;
        $product->img_3      = $product->img_3 ? $baseImgPath . $product->img_3 : null;
        $product->img_4      = $product->img_4 ? $baseImgPath . $product->img_4 : null;

        return view('website.products.show', compact('product'));
    }

}
