<?php

namespace App\Http\Controllers\Membership\Public;

use App\Http\Controllers\Controller;
use App\Models\MembershipCategory;
use App\Models\LoanProduct;
use Illuminate\Http\Request;

class MembershipCategoryController extends Controller
{
    /**
     * Display all membership categories.
     */
    public function index()
    {
        $categories = MembershipCategory::active()
            ->ordered()
            ->with(['subscriptionFees' => function ($query) {
                $query->active()->default();
            }])
            ->get();

        $loanProducts = LoanProduct::active()
            ->ordered()
            ->get();

        return view('membership.public.categories', compact('categories', 'loanProducts'));
    }

    /**
     * Display a specific membership category.
     */
    public function show(MembershipCategory $membershipCategory)
    {
        if (!$membershipCategory->is_active) {
            abort(404);
        }

        $membershipCategory->load([
            'subscriptionFees' => function ($query) {
                $query->active();
            },
            'contributionFees' => function ($query) {
                $query->active();
            },
        ]);

        return view('membership.public.category-show', compact('membershipCategory'));
    }
}
