<?php

namespace App\Http\Controllers;

use App\Models\VarietyVariation;
use App\Models\GrapeVariety;
use Illuminate\Http\Request;

class VarietyVariationController extends Controller
{
    /**
     * Display a listing of variety variations.
     */
    public function index()
    {
        $variations = VarietyVariation::with('grapeVariety')->paginate(15);

        return view('variety-variations.index', compact('variations'));
    }

    /**
     * Show the form for creating a new variety variation.
     */
    public function create()
    {
        $grapeVarieties = GrapeVariety::all();

        return view('variety-variations.create', compact('grapeVarieties'));
    }

    /**
     * Store a newly created variety variation in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'grape_variety_id' => 'required|exists:grape_varieties,id',
            'color' => 'required|in:green,blue,yellow,red',
            'description' => 'nullable|string',
            'typical_sugar_content' => 'nullable|numeric|min:0|max:50',
            'typical_alcohol' => 'nullable|numeric|min:0|max:20',
            'ripening_period' => 'nullable|string|max:255',
            'status' => 'required|in:active,inactive',
        ]);

        $variation = VarietyVariation::create($validated);

        return redirect()->route('variety-variations.show', $variation)
            ->with('success', 'Variety variation created successfully.');
    }

    /**
     * Display the specified variety variation.
     */
    public function show(VarietyVariation $varietyVariation)
    {
        $varietyVariation->load('grapeVariety', 'vineyardRows', 'harvests');

        return view('variety-variations.show', compact('varietyVariation'));
    }

    /**
     * Show the form for editing the specified variety variation.
     */
    public function edit(VarietyVariation $varietyVariation)
    {
        $grapeVarieties = GrapeVariety::all();

        return view('variety-variations.edit', compact('varietyVariation', 'grapeVarieties'));
    }

    /**
     * Update the specified variety variation in storage.
     */
    public function update(Request $request, VarietyVariation $varietyVariation)
    {
        $validated = $request->validate([
            'grape_variety_id' => 'required|exists:grape_varieties,id',
            'color' => 'required|in:green,blue,yellow,red',
            'description' => 'nullable|string',
            'typical_sugar_content' => 'nullable|numeric|min:0|max:50',
            'typical_alcohol' => 'nullable|numeric|min:0|max:20',
            'ripening_period' => 'nullable|string|max:255',
            'status' => 'required|in:active,inactive',
        ]);

        $varietyVariation->update($validated);

        return redirect()->route('variety-variations.show', $varietyVariation)
            ->with('success', 'Variety variation updated successfully.');
    }

    /**
     * Remove the specified variety variation from storage.
     */
    public function destroy(VarietyVariation $varietyVariation)
    {
        $varietyVariation->delete();

        return redirect()->route('variety-variations.index')
            ->with('success', 'Variety variation deleted successfully.');
    }
}
