<?php

namespace App\Http\Controllers\Api\V1;

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

class BarobillController extends Controller
{
    public function __construct(
        private BarobillService $barobillService
    ) {}

    /**
     * 연동 현황 조회
     */
    public function status()
    {
        return ApiResponse::handle(function () {
            $setting = $this->barobillService->getSetting();

            return [
                'bank_service_count' => 0,
                'account_link_count' => 0,
                'member' => $setting ? [
                    'barobill_id' => $setting->barobill_id,
                    'biz_no' => $setting->corp_num,
                    'status' => $setting->isVerified() ? 'active' : 'inactive',
                    'server_mode' => config('services.barobill.test_mode', true) ? 'test' : 'production',
                ] : null,
            ];
        }, __('message.fetched'));
    }

    /**
     * 바로빌 로그인 정보 등록
     */
    public function login(Request $request)
    {
        $data = $request->validate([
            'barobill_id' => 'required|string',
            'password' => 'required|string',
        ]);

        return ApiResponse::handle(function () use ($data) {
            return $this->barobillService->saveSetting([
                'barobill_id' => $data['barobill_id'],
            ]);
        }, __('message.saved'));
    }

    /**
     * 바로빌 회원가입 정보 등록
     */
    public function signup(Request $request)
    {
        $data = $request->validate([
            'business_number' => 'required|string|size:10',
            'company_name' => 'required|string',
            'ceo_name' => 'required|string',
            'business_type' => 'nullable|string',
            'business_category' => 'nullable|string',
            'address' => 'nullable|string',
            'barobill_id' => 'required|string',
            'password' => 'required|string',
            'manager_name' => 'nullable|string',
            'manager_phone' => 'nullable|string',
            'manager_email' => 'nullable|email',
        ]);

        return ApiResponse::handle(function () use ($data) {
            return $this->barobillService->saveSetting([
                'corp_num' => $data['business_number'],
                'corp_name' => $data['company_name'],
                'ceo_name' => $data['ceo_name'],
                'biz_type' => $data['business_type'] ?? null,
                'biz_class' => $data['business_category'] ?? null,
                'addr' => $data['address'] ?? null,
                'barobill_id' => $data['barobill_id'],
                'contact_name' => $data['manager_name'] ?? null,
                'contact_tel' => $data['manager_phone'] ?? null,
                'contact_id' => $data['manager_email'] ?? null,
            ]);
        }, __('message.saved'));
    }

    /**
     * 은행 빠른조회 서비스 URL 조회
     */
    public function bankServiceUrl(Request $request)
    {
        return ApiResponse::handle(function () {
            $baseUrl = config('services.barobill.test_mode', true)
                ? 'https://testws.barobill.co.kr'
                : 'https://ws.barobill.co.kr';

            return ['url' => $baseUrl.'/Bank/BankAccountService'];
        }, __('message.fetched'));
    }

    /**
     * 계좌 연동 등록 URL 조회
     */
    public function accountLinkUrl()
    {
        return ApiResponse::handle(function () {
            $baseUrl = config('services.barobill.test_mode', true)
                ? 'https://testws.barobill.co.kr'
                : 'https://ws.barobill.co.kr';

            return ['url' => $baseUrl.'/Bank/AccountLink'];
        }, __('message.fetched'));
    }

    /**
     * 카드 연동 등록 URL 조회
     */
    public function cardLinkUrl()
    {
        return ApiResponse::handle(function () {
            $baseUrl = config('services.barobill.test_mode', true)
                ? 'https://testws.barobill.co.kr'
                : 'https://ws.barobill.co.kr';

            return ['url' => $baseUrl.'/Card/CardLink'];
        }, __('message.fetched'));
    }

    /**
     * 공인인증서 등록 URL 조회
     */
    public function certificateUrl()
    {
        return ApiResponse::handle(function () {
            $baseUrl = config('services.barobill.test_mode', true)
                ? 'https://testws.barobill.co.kr'
                : 'https://ws.barobill.co.kr';

            return ['url' => $baseUrl.'/Certificate/Register'];
        }, __('message.fetched'));
    }
}
