<?php

namespace App\Modules\SuperAdmin\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Responses\ApiResponse;
use App\Modules\Subscription\Models\Subscription;
use App\Modules\SuperAdmin\Requests\UpdateSubscriptionRequest;
use App\Modules\SuperAdmin\Resources\AdminSubscriptionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SubscriptionController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $subscriptions = Subscription::query()
            ->with(['company', 'plan'])
            ->when($request->query('status'), fn ($query, string $status) => $query->where('status', $status))
            ->latest()
            ->paginate((int) $request->query('per_page', 15));

        return ApiResponse::success(AdminSubscriptionResource::collection($subscriptions));
    }

    public function update(UpdateSubscriptionRequest $request, Subscription $subscription): JsonResponse
    {
        $subscription->update($request->validated());

        return ApiResponse::success(
            new AdminSubscriptionResource($subscription->load(['company', 'plan'])),
            'Subscription updated successfully'
        );
    }
}
