<?php

namespace App\Http\Controllers\Payment;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Payment\IndexPaymentRequest;
use App\Http\Requests\Payment\SaveTenantPaymentSettingRequest;
use App\Http\Requests\Payment\StoreGatewayAccountRequest;
use App\Http\Resources\Payment\PaymentTransactionResource;
use App\Models\PaymentGatewayAccount;
use App\Services\Payment\PaymentGatewayService;
use Illuminate\Http\Request;

class PaymentGatewayController extends ApiController
{
    public function __construct(protected PaymentGatewayService $service) {}

    public function providers()
    {
        return $this->success($this->service->providers(), 'Daftar provider berhasil diambil');
    }

    public function settings(Request $request)
    {
        return $this->success($this->service->settings($request->user(), $this->tenantId($request)), 'Pengaturan payment gateway berhasil diambil');
    }

    public function storeAccount(StoreGatewayAccountRequest $request)
    {
        return $this->created($this->service->saveAccount($request->validated(), $request->user(), $this->tenantId($request)), 'Akun gateway berhasil dibuat');
    }

    public function updateAccount(StoreGatewayAccountRequest $request, PaymentGatewayAccount $account)
    {
        return $this->updated($this->service->saveAccount($request->validated(), $request->user(), $this->tenantId($request), $account), 'Akun gateway berhasil diperbarui');
    }

    public function saveSetting(SaveTenantPaymentSettingRequest $request)
    {
        return $this->updated($this->service->saveTenantSetting($request->validated(), $request->user(), $this->tenantId($request)), 'Mode pembayaran tenant berhasil disimpan');
    }

    public function transactions(IndexPaymentRequest $request)
    {
        return $this->successPaginated($this->service->transactions($request->validated(), $request->user(), $this->tenantId($request)), PaymentTransactionResource::class, 'Log pembayaran berhasil diambil');
    }

    private function tenantId(Request $request): ?int
    {
        return $request->attributes->get('tenant_id');
    }
}
