<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MovimentoBancario;
use App\Models\Banca;
use Illuminate\Http\Request;

class MovimentoBancarioController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $movimenti = MovimentoBancario::with('banca')->latest()->paginate(15);
        return view('admin.movimenti-bancari.index', compact('movimenti'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $banche = Banca::all();
        return view('admin.movimenti-bancari.create', compact('banche'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'banca_id' => 'required|exists:banche,id',
            'data_movimento' => 'required|date',
            'tipo_movimento' => 'required|in:entrata,uscita',
            'importo' => 'required|numeric|min:0',
            'causale' => 'required|string|max:255',
            'riferimento' => 'nullable|string|max:100',
            'note' => 'nullable|string'
        ]);

        MovimentoBancario::create($validated);

        return redirect()->route('admin.movimenti-bancari.index')
            ->with('success', 'Movimento bancario creato con successo.');
    }

    /**
     * Display the specified resource.
     */
    public function show(MovimentoBancario $movimentoBancario)
    {
        $movimentoBancario->load('banca');
        return view('admin.movimenti-bancari.show', compact('movimentoBancario'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(MovimentoBancario $movimentoBancario)
    {
        $banche = Banca::all();
        return view('admin.movimenti-bancari.edit', compact('movimentoBancario', 'banche'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, MovimentoBancario $movimentoBancario)
    {
        $validated = $request->validate([
            'banca_id' => 'required|exists:banche,id',
            'data_movimento' => 'required|date',
            'tipo_movimento' => 'required|in:entrata,uscita',
            'importo' => 'required|numeric|min:0',
            'causale' => 'required|string|max:255',
            'riferimento' => 'nullable|string|max:100',
            'note' => 'nullable|string'
        ]);

        $movimentoBancario->update($validated);

        return redirect()->route('admin.movimenti-bancari.index')
            ->with('success', 'Movimento bancario aggiornato con successo.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(MovimentoBancario $movimentoBancario)
    {
        $movimentoBancario->delete();

        return redirect()->route('admin.movimenti-bancari.index')
            ->with('success', 'Movimento bancario eliminato con successo.');
    }
}
