<?php

namespace App\Http\Controllers;

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

class InstallmentController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(Installment $installment)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Installment $installment)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Installment $installment)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Installment $installment)
    {
        // Add authorization if needed
        if (!auth()->user()->hasRole('Superadmin') && !auth()->user()->can('delete_installment_plans')) {
            abort(403, 'Unauthorized action.');
        }

        // Delete associated invoices if any (optional, or just handle foreign keys in DB)
        // \App\Models\Invoice::where('installment_id', $installment->id)->delete();

        $installment->delete();

        return back()->with('success', 'Installment deleted successfully.');
    }
}
