<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\AjoGroupResource;
use App\Http\Resources\ContributionResource;
use App\Http\Resources\MemberResource;
use App\Http\Resources\PayoutResource;
use App\Models\AjoGroup;
use App\Models\Contribution;
use App\Models\Member;
use App\Models\Payout;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class PlatformController extends Controller
{
    public function groups(): AnonymousResourceCollection
    {
        $this->authorize('viewAny', AjoGroup::class);

        return AjoGroupResource::collection(AjoGroup::with('business:id,currency')->withCount('members')->latest()->paginate(20));
    }

    public function members(): AnonymousResourceCollection
    {
        $this->authorize('viewAny', Member::class);

        return MemberResource::collection(Member::latest()->paginate(20));
    }

    public function contributions(): AnonymousResourceCollection
    {
        $this->authorize('viewAny', Contribution::class);

        return ContributionResource::collection(Contribution::with(['group', 'member'])->latest('due_date')->paginate(20));
    }

    public function payouts(): AnonymousResourceCollection
    {
        $this->authorize('viewAny', Payout::class);

        return PayoutResource::collection(Payout::with(['group', 'member'])->latest('scheduled_date')->paginate(20));
    }
}
