<?php

namespace App\Http\Controllers\ParentPortal;

use App\Http\Controllers\Controller;
use App\Http\Controllers\ParentPortal\Concerns\ResolvesFamily;
use App\Models\FeeChallan;
use Illuminate\View\View;

class FeeController extends Controller
{
    use ResolvesFamily;

    public function index(): View
    {
        $family = $this->family();

        $challans = FeeChallan::where('family_id', $family->id)
            ->with('items.student', 'items.feeType')->latest()->get();

        $outstanding = $challans->whereIn('status', ['unpaid', 'partial'])->sum(fn ($c) => $c->balance);
        $paid = $challans->sum('paid_amount');

        return view('parent.fees', compact('family', 'challans', 'outstanding', 'paid'));
    }

    public function show(FeeChallan $challan): View
    {
        abort_unless($challan->family_id === $this->family()->id, 403);
        $challan->load(['items.student', 'items.feeType', 'payments']);

        return view('parent.fee-detail', compact('challan'));
    }
}
