<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\RecordPaymentRequest;
use App\Models\Invoice;
use App\Services\Billing\PaymentRecordService;
use App\Services\Tenancy\CurrentBusiness;
use Illuminate\Http\RedirectResponse;

class InvoicePaymentController extends Controller
{
    public function store(RecordPaymentRequest $request, Invoice $invoice, CurrentBusiness $currentBusiness, PaymentRecordService $payments): RedirectResponse
    {
        $business = $currentBusiness->get() ?? abort(403);

        abort_unless((int) $invoice->business_id === (int) $business->id, 404);

        $payments->record($business, $invoice, $request->payload());

        return redirect()
            ->route('admin.invoices.show', $invoice)
            ->with('status', 'Payment recorded successfully.');
    }
}
