<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Order\StorePaymentRequest;
use App\Http\Resources\OrderResource;
use App\Models\Order;
use App\Services\OrderService;

class PaymentController extends ApiController
{
    public function __construct(private readonly OrderService $orderService)
    {
    }

    public function store(StorePaymentRequest $request, Order $order): OrderResource
    {
        $this->ensureCompanyScoped($order, $this->currentCompanyId($request));
        $this->ensureBranchScoped($order, $this->currentBranchId($request));

        $order = $this->orderService->addPayments(
            $this->currentUser($request),
            $this->currentBranchId($request),
            $order,
            $request->validated('payments'),
        );

        return new OrderResource($order);
    }
}
