<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\BankAccount\StoreBankAccountRequest;
use App\Http\Requests\V1\BankAccount\UpdateBankAccountRequest;
use App\Services\BankAccountService;
use Illuminate\Http\Request;

class BankAccountController extends Controller
{
    public function __construct(
        private readonly BankAccountService $service
    ) {}

    /**
     * 계좌 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'status',
            'assigned_user_id',
            'is_primary',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

        $accounts = $this->service->index($params);

        return ApiResponse::success($accounts, __('message.fetched'));
    }

    /**
     * 계좌 등록
     */
    public function store(StoreBankAccountRequest $request)
    {
        $account = $this->service->store($request->validated());

        return ApiResponse::success($account, __('message.created'), [], 201);
    }

    /**
     * 계좌 상세
     */
    public function show(int $id)
    {
        $account = $this->service->show($id);

        return ApiResponse::success($account, __('message.fetched'));
    }

    /**
     * 계좌 수정
     */
    public function update(int $id, UpdateBankAccountRequest $request)
    {
        $account = $this->service->update($id, $request->validated());

        return ApiResponse::success($account, __('message.updated'));
    }

    /**
     * 계좌 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

        return ApiResponse::success(null, __('message.deleted'));
    }

    /**
     * 계좌 상태 토글 (사용/정지)
     */
    public function toggle(int $id)
    {
        $account = $this->service->toggleStatus($id);

        return ApiResponse::success($account, __('message.updated'));
    }

    /**
     * 대표계좌 설정
     */
    public function setPrimary(int $id)
    {
        $account = $this->service->setPrimary($id);

        return ApiResponse::success($account, __('message.updated'));
    }

    /**
     * 활성 계좌 목록 (셀렉트박스용)
     */
    public function active()
    {
        $accounts = $this->service->getActiveAccounts();

        return ApiResponse::success($accounts, __('message.fetched'));
    }
}
