<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\BankTransactionService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 은행 거래 (입출금 통합) 조회 컨트롤러
 */
class BankTransactionController extends Controller
{
    public function __construct(
        protected BankTransactionService $service
    ) {}

    /**
     * 입출금 통합 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            $params = $request->validate([
                'start_date' => 'nullable|date',
                'end_date' => 'nullable|date|after_or_equal:start_date',
                'bank_account_id' => 'nullable|integer',
                'transaction_type' => 'nullable|string|max:50',
                'search' => 'nullable|string|max:100',
                'sort_by' => 'nullable|in:transaction_date,amount',
                'sort_dir' => 'nullable|in:asc,desc',
                'per_page' => 'nullable|integer|min:1|max:100',
                'page' => 'nullable|integer|min:1',
            ]);

            return $this->service->index($params);
        }, __('message.fetched'));
    }

    /**
     * 입출금 요약 통계
     */
    public function summary(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            $params = $request->validate([
                'start_date' => 'nullable|date',
                'end_date' => 'nullable|date|after_or_equal:start_date',
            ]);

            return $this->service->summary($params);
        }, __('message.fetched'));
    }

    /**
     * 계좌 목록 조회 (필터용)
     */
    public function accounts(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->getAccountOptions();
        }, __('message.fetched'));
    }
}
