<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\ProductFeature;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::orderBy('sort_order')->get();
        return view('admin.pages.products.index', compact('products'));
    }

    public function create()
    {
        return view('admin.pages.products.form');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
        ]);

        $data = $request->only([
            'name','subtitle','standard_badge','short_description',
            'full_description','sort_order','is_active','meta_title','meta_description'
        ]);
        $data['slug'] = Str::slug($data['name']);

        // Handle file uploads
        foreach (['hero_image','thumbnail'] as $field) {
            if ($request->hasFile($field)) {
                $data[$field] = $request->file($field)->store('images', 'public');
                $data[$field] = basename($data[$field]);
            }
        }
        if ($request->hasFile('brochure_file')) {
            $data['brochure_file'] = basename($request->file('brochure_file')->store('brochures', 'public'));
        }

        $product = Product::create($data);
        $this->syncFeatures($product, $request->features ?? []);

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

    public function edit(Product $product)
    {
        $product->load('features', 'images');
        return view('admin.pages.products.form', compact('product'));
    }

    public function update(Request $request, Product $product)
    {
        $request->validate(['name' => 'required|string|max:255']);

        $data = $request->only([
            'name','subtitle','standard_badge','short_description',
            'full_description','sort_order','is_active','meta_title','meta_description'
        ]);
        $data['slug'] = Str::slug($data['name']);

        foreach (['hero_image','thumbnail'] as $field) {
            if ($request->hasFile($field)) {
                $data[$field] = basename($request->file($field)->store('images', 'public'));
            }
        }
        if ($request->hasFile('brochure_file')) {
            $data['brochure_file'] = basename($request->file('brochure_file')->store('brochures', 'public'));
        }

        $product->update($data);
        $this->syncFeatures($product, $request->features ?? []);

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

    public function destroy(Product $product)
    {
        $product->delete();
        return redirect()->route('admin.products.index')->with('success', 'Product deleted.');
    }

    private function syncFeatures(Product $product, array $features)
    {
        $product->features()->delete();
        foreach (array_filter($features) as $i => $feat) {
            $product->features()->create(['feature' => $feat, 'sort_order' => $i]);
        }
    }
}
