<?php

namespace App\Http\Controllers;

use App\Models\Impact;
use Illuminate\Http\Request;

class ImpactController extends Controller
{
    public function edit()
    {
        $impacts = Impact::orderBy('order')->get();
        return view('pages.impact-edit', compact('impacts'));
    }

    public function update(Request $request)
    {
        $request->validate([
            'impacts' => 'required|array',
            'impacts.*.id' => 'required|exists:impacts,id',
            'impacts.*.label' => 'required|string|max:255',
            'impacts.*.value' => 'required|string|max:255',
        ]);

        foreach ($request->impacts as $data) {
            Impact::where('id', $data['id'])->update([
                'label' => $data['label'],
                'value' => $data['value'],
            ]);
        }

        return redirect()->route('home')->with('success', 'Impact stats updated successfully.');
    }
}
