<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Admin\Services\ProductService;
use Modules\Admin\Services\CategoryService;
use Modules\Media\Services\MediaService;

class ProductController extends Controller
{
    public function __construct(
        private ProductService $productService,
        private CategoryService $categoryService,
        private MediaService $mediaService
    ) {}

    public function index()
    {
        $products = $this->productService->getAll();
        return view('admin::products.index', compact('products'));
    }

    public function create()
    {
        $categories = $this->categoryService->getAll(100);
        return view('admin::products.create', compact('categories'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|unique:products,slug',
            'description' => 'nullable|string',
            'price' => 'required|numeric|min:0',
            'unit' => 'nullable|string|max:50',
            'unit_price' => 'nullable|numeric|min:0',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'nutri_score' => 'nullable|string|max:10',
            'brand' => 'nullable|string|max:255',
            'discount' => 'nullable|numeric|min:0|max:100',
            'original_price' => 'nullable|numeric|min:0',
            'category_id' => 'nullable|exists:categories,id',
            'is_active' => 'boolean',
            'is_save_the_food' => 'boolean',
            'is_weekly_special' => 'boolean',
            'stock_quantity' => 'integer|min:0',
            'min_order_quantity' => 'integer|min:1',
            'max_order_quantity' => 'integer|min:1',
            'ingredients' => 'nullable|string',
            'allergens_info' => 'nullable|string',
        ]);

        $product = $this->productService->create($data);

        if ($request->hasFile('image')) {
            $this->mediaService->upload($request->file('image'), 'image', $product);
        }

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

    public function show(int $id)
    {
        $product = $this->productService->findById($id);
        if (!$product) {
            return redirect()->route('admin.products.index')->with('error', 'Product not found.');
        }
        return view('admin::products.show', compact('product'));
    }

    public function edit(int $id)
    {
        $product = $this->productService->findById($id);
        if (!$product) {
            return redirect()->route('admin.products.index')->with('error', 'Product not found.');
        }
        $product->load('media');
        $categories = $this->categoryService->getAll(100);
        return view('admin::products.edit', compact('product', 'categories'));
    }

    public function update(Request $request, int $id)
    {
        $data = $request->validate([
            'name' => 'string|max:255',
            'slug' => 'string|unique:products,slug,' . $id,
            'description' => 'nullable|string',
            'price' => 'numeric|min:0',
            'unit' => 'nullable|string|max:50',
            'unit_price' => 'nullable|numeric|min:0',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'nutri_score' => 'nullable|string|max:10',
            'brand' => 'nullable|string|max:255',
            'discount' => 'nullable|numeric|min:0|max:100',
            'original_price' => 'nullable|numeric|min:0',
            'category_id' => 'nullable|exists:categories,id',
            'is_active' => 'boolean',
            'is_save_the_food' => 'boolean',
            'is_weekly_special' => 'boolean',
            'stock_quantity' => 'integer|min:0',
            'min_order_quantity' => 'integer|min:1',
            'max_order_quantity' => 'integer|min:1',
            'ingredients' => 'nullable|string',
            'allergens_info' => 'nullable|string',
        ]);

        $product = $this->productService->update($id, $data);

        if ($request->hasFile('image')) {
            $product->load('media');
            foreach ($product->media as $m) {
                $this->mediaService->delete($m->id);
            }
            $this->mediaService->upload($request->file('image'), 'image', $product);
        }

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

    public function destroy(int $id)
    {
        $product = $this->productService->findById($id);
        if ($product) {
            $product->load('media');
            foreach ($product->media as $m) {
                $this->mediaService->delete($m->id);
            }
        }
        $this->productService->delete($id);
        return redirect()->route('admin.products.index')
            ->with('success', 'Product deleted successfully.');
    }
}
