<?php

namespace App\Http\Controllers\Admin;

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

class ProductVariationController extends Controller
{
    public function index()
    {
        $productVariations = ProductVariation::with('product', 'variationType')->latest()->paginate(20);
        return view('admin.product-variations.index', compact('productVariations'));
    }

    public function create()
    {
        $products = Product::active()->get();
        $variationTypes = VariationType::all();
        return view('admin.product-variations.create', compact('products', 'variationTypes'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'product_id' => 'required|exists:products,id',
            'variation_type_id' => 'required|exists:variation_types,id',
            'value' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'stock_quantity' => 'required|integer|min:0',
            'sku' => 'nullable|string|max:255|unique:product_variations,sku',
            'is_active' => 'boolean',
        ]);

        if (empty($validated['sku'])) {
            $validated['sku'] = strtoupper(Str::random(10));
        }

        $validated['is_active'] = $request->boolean('is_active');

        ProductVariation::create($validated);

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

    public function edit(ProductVariation $productVariation)
    {
        $products = Product::active()->get();
        $variationTypes = VariationType::all();
        return view('admin.product-variations.edit', compact('productVariation', 'products', 'variationTypes'));
    }

    public function update(Request $request, ProductVariation $productVariation)
    {
        $validated = $request->validate([
            'product_id' => 'required|exists:products,id',
            'variation_type_id' => 'required|exists:variation_types,id',
            'value' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'stock_quantity' => 'required|integer|min:0',
            'sku' => 'nullable|string|max:255|unique:product_variations,sku,' . $productVariation->id,
            'is_active' => 'boolean',
        ]);

        $validated['is_active'] = $request->boolean('is_active');

        $productVariation->update($validated);

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

    public function destroy(ProductVariation $productVariation)
    {
        $productVariation->delete();

        return redirect()->route('admin.product-variations.index')
            ->with('success', 'Product variation deleted successfully.');
    }
}
