<?php

namespace App\Http\Controllers\Admin;

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

class BancaController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $banche = Banca::with(['movimentiBancari'])
            ->orderBy('denominazione')
            ->paginate(15);

        return view('admin.banche.index', compact('banche'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.banche.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'denominazione' => 'required|string|max:255',
            'codice_abi' => 'nullable|string|max:10',
            'codice_cab' => 'nullable|string|max:10',
            'iban' => 'nullable|string|max:34',
            'indirizzo' => 'nullable|string',
            'telefono' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
            'note' => 'nullable|string',
            'attivo' => 'boolean',
        ]);

        $banca = Banca::create($validated);

        return redirect()
            ->route('admin.banche.index')
            ->with('success', 'Banca creata con successo.');
    }

    /**
     * Display the specified resource.
     */
    public function show(Banca $banca)
    {
        $banca->load(['movimentiBancari' => function($query) {
            $query->orderBy('data_operazione', 'desc')->limit(10);
        }]);

        return view('admin.banche.show', compact('banca'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Banca $banca)
    {
        return view('admin.banche.edit', compact('banca'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Banca $banca)
    {
        $validated = $request->validate([
            'denominazione' => 'required|string|max:255',
            'codice_abi' => 'nullable|string|max:10',
            'codice_cab' => 'nullable|string|max:10',
            'iban' => 'nullable|string|max:34',
            'indirizzo' => 'nullable|string',
            'telefono' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
            'note' => 'nullable|string',
            'attivo' => 'boolean',
        ]);

        $banca->update($validated);

        return redirect()
            ->route('admin.banche.index')
            ->with('success', 'Banca aggiornata con successo.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Banca $banca)
    {
        // Check if bank has any movements before deleting
        if ($banca->movimentiBancari()->count() > 0) {
            return redirect()
                ->route('admin.banche.index')
                ->with('error', 'Impossibile eliminare la banca: sono presenti movimenti associati.');
        }

        $banca->delete();

        return redirect()
            ->route('admin.banche.index')
            ->with('success', 'Banca eliminata con successo.');
    }
}
