<?php

namespace App\Http\Controllers;

use App\Models\WineProduction;
use App\Models\Wine;
use App\Models\Harvest;
use Illuminate\Http\Request;

class WineProductionController extends Controller
{
    /**
     * Display a listing of wine production records.
     */
    public function index()
    {
        $productions = WineProduction::with('wine', 'harvest')->paginate(15);

        return view('wine-productions.index', compact('productions'));
    }

    /**
     * Show the form for creating a new wine production record.
     */
    public function create()
    {
        $wines = Wine::all();
        $harvests = Harvest::with('varietyVariation.grapeVariety')->get();

        return view('wine-productions.create', compact('wines', 'harvests'));
    }

    /**
     * Store a newly created wine production record in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'wine_id' => 'required|exists:wines,id',
            'harvest_id' => 'required|exists:harvests,id',
            'consumed_weight' => 'required|numeric|min:0',
            'blend_percentage' => 'nullable|numeric|min:0|max:100',
        ]);

        $production = WineProduction::create($validated);

        return redirect()->route('wine-productions.show', $production)
            ->with('success', 'Wine production record created successfully.');
    }

    /**
     * Display the specified wine production record.
     */
    public function show(WineProduction $wineProduction)
    {
        $wineProduction->load('wine.grapeVariety', 'harvest.varietyVariation');

        return view('wine-productions.show', compact('wineProduction'));
    }

    /**
     * Show the form for editing the specified wine production record.
     */
    public function edit(WineProduction $wineProduction)
    {
        $wines = Wine::all();
        $harvests = Harvest::with('varietyVariation.grapeVariety')->get();

        return view('wine-productions.edit', compact('wineProduction', 'wines', 'harvests'));
    }

    /**
     * Update the specified wine production record in storage.
     */
    public function update(Request $request, WineProduction $wineProduction)
    {
        $validated = $request->validate([
            'wine_id' => 'required|exists:wines,id',
            'harvest_id' => 'required|exists:harvests,id',
            'consumed_weight' => 'required|numeric|min:0',
            'blend_percentage' => 'nullable|numeric|min:0|max:100',
        ]);

        $wineProduction->update($validated);

        return redirect()->route('wine-productions.show', $wineProduction)
            ->with('success', 'Wine production record updated successfully.');
    }

    /**
     * Remove the specified wine production record from storage.
     */
    public function destroy(WineProduction $wineProduction)
    {
        $wineProduction->delete();

        return redirect()->route('wine-productions.index')
            ->with('success', 'Wine production record deleted successfully.');
    }
}
