<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Payment\PaymentActionRequest;
use App\Http\Requests\V1\Payment\PaymentIndexRequest;
use App\Http\Requests\V1\Payment\PaymentStoreRequest;
use App\Services\PaymentService;
use Illuminate\Http\JsonResponse;

class PaymentController extends Controller
{
    public function __construct(
        private readonly PaymentService $paymentService
    ) {}

    /**
     * 결제 목록
     */
    public function index(PaymentIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->index($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 결제 요약 통계
     */
    public function summary(PaymentIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->summary($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 결제 상세
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->show($id),
            __('message.fetched')
        );
    }

    /**
     * 결제 등록 (수동)
     */
    public function store(PaymentStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->store($request->validated()),
            __('message.created')
        );
    }

    /**
     * 결제 완료 처리
     */
    public function complete(PaymentActionRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->complete($id, $request->validated()['transaction_id'] ?? null),
            __('message.payment.completed')
        );
    }

    /**
     * 결제 취소
     */
    public function cancel(PaymentActionRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->cancel($id, $request->validated()['reason'] ?? null),
            __('message.payment.cancelled')
        );
    }

    /**
     * 환불 처리
     */
    public function refund(PaymentActionRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->refund($id, $request->validated()['reason'] ?? null),
            __('message.payment.refunded')
        );
    }

    /**
     * 결제 명세서 조회
     */
    public function statement(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->paymentService->statement($id),
            __('message.fetched')
        );
    }
}
