<?php

namespace App\Http\Controllers\admin;

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

class ProductController extends Controller
{
   public function index()
    {
        $products = Product::with('category')->get();
        return view('admin.product.index', compact('products'));
    }
    public function create() {
        $categories = Category::all(); 
        return view('admin.product.create', compact('categories'));
    }

    

public function store(Request $request) {
    $request->validate([
        'name' => 'required|string|max:255',
        'price' => 'required|numeric|min:0',
        'stock' => 'required|integer|min:0',
        'category' => 'required|exists:categories,id',
        'description' => 'required|string',
        'image_url' => 'nullable|file|mimes:jpeg,png,jpg,gif|max:2048', 
    ]);

    $product = new Product;
    $product->name = $request->name;
    
    // Generate and assign the slug from the product name
    $product->slug = Str::slug($request->name); 
    
    $product->price = $request->price;
    $product->stock = $request->stock;
    $product->category_id = $request->category;
    $product->description = $request->description;

    if ($request->hasFile('image_url')) {
        $imageName = time().'.'.$request->image_url->extension();  
        $request->image_url->move(public_path('images'), $imageName);
        $product->image = 'images/'.$imageName;
    }

    $product->save();

    return redirect()->route('admin.product')->with('success', 'Product created successfully!');
}



 public function edit($id)
    {
        $categories = Category::all();
        $product = Product::findOrFail($id);
        return view('admin.product.edit', compact('product', 'categories'));
    }
   public function update(Request $request, $id)
{
    $product = Product::findOrFail($id);

    $request->validate([
        'name' => 'required|string|max:255|unique:products,name,' . $id,
        'price' => 'required|numeric|min:0',
        'stock' => 'required|integer|min:0',
        'category' => 'required|exists:categories,id',
        'description' => 'required|string',
        'image_url' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);

    $product->name        = $request->name;
    $product->slug        = Str::slug($request->name);
    $product->price       = $request->price;
    $product->stock       = $request->stock;
    $product->category_id = $request->category;
    $product->description = $request->description;
    if ($request->hasFile('image_url')) {
        $imageName = time().'.'.$request->image_url->extension();  
        $request->image_url->move(public_path('images'), $imageName);
        $product->image = 'images/'.$imageName;
    }

    $product->save();

    return redirect()->route('admin.product')->with('success', 'Product updated successfully!');
}

public function destroy($id)
{
    $product = Product::findOrFail($id);
    if ($product->image && file_exists(public_path($product->image))) {
        unlink(public_path($product->image));
    }
    $product->delete();
    return redirect()->route('admin.product')
                     ->with('success', 'Product deleted successfully!');
}


}
