<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\BillingHistory;
use App\Models\Subscription;
use App\Support\ApiResponse;
use Illuminate\Http\Request;

class BillingController extends Controller
{
    public function show(Request $request, int $businessId)
    {
        $sub = Subscription::where('business_id', $businessId)->latest()->first();
        return ApiResponse::success('Billing info', [
            'subscription' => $sub,
        ]);
    }

    public function history(Request $request, int $businessId)
    {
        $items = BillingHistory::where('business_id', $businessId)->orderByDesc('billing_date')->get();
        return ApiResponse::success('Billing history', $items);
    }

    public function usage(Request $request, int $businessId)
    {
        $sub = Subscription::where('business_id', $businessId)->latest()->first();
        return ApiResponse::success('Usage', [
            'limit' => $sub?->document_limit ?? 0,
            'used' => $sub?->documents_used ?? 0,
        ]);
    }

    public function subscription(Request $request, int $businessId)
    {
        $sub = Subscription::where('business_id', $businessId)->latest()->first();
        return ApiResponse::success('Subscription', $sub);
    }
}
