<?php

namespace App\Http\Controllers\Api;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\PaymentRequest;
use App\Http\Resources\PaymentResource;
use App\Models\Payment;
use App\Services\PaymentService;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    protected $service;

    public function __construct(PaymentService $service)
    {
        $this->service = $service;
    }

    public function index(Request $request)
    {
        $payments = $this->service->list($request);

        return ApiResponse::pagination(
            $payments,
            PaymentResource::class,
            'Payments fetched successfully.'
        );
    }

    public function store(PaymentRequest $request)
    {
        $payment = $this->service->store($request);

        return ApiResponse::success(
            'Payment received successfully.',
            new PaymentResource($payment)
        );
    }

    public function show(Payment $payment)
    {
        return ApiResponse::success(
            'Payment fetched successfully.',
            new PaymentResource(
                $this->service->show($payment)
            )
        );
    }

    public function destroy(Payment $payment)
    {
        $this->service->destroy($payment);

        return ApiResponse::success(
            'Payment deleted successfully.'
        );
    }
}