<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Company\CheckBusinessNumberRequest;
use App\Http\Requests\V1\Company\CompanyRequestActionRequest;
use App\Http\Requests\V1\Company\CompanyRequestIndexRequest;
use App\Http\Requests\V1\Company\CompanyRequestStoreRequest;
use App\Services\CompanyService;
use Illuminate\Http\JsonResponse;

class CompanyController extends Controller
{
    public function __construct(
        private readonly CompanyService $companyService
    ) {}

    /**
     * 사업자등록번호 유효성 검사
     */
    public function check(CheckBusinessNumberRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->checkBusinessNumber(
                $request->validated()['business_number']
            ),
            __('message.company.checked')
        );
    }

    /**
     * 회사 추가 신청
     */
    public function request(CompanyRequestStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->createRequest($request->validated()),
            __('message.company.request_created')
        );
    }

    /**
     * 회사 추가 신청 목록 (관리자용)
     */
    public function requests(CompanyRequestIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->getRequests($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 회사 추가 신청 상세
     */
    public function showRequest(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->getRequest($id),
            __('message.fetched')
        );
    }

    /**
     * 회사 추가 신청 승인
     */
    public function approve(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->approveRequest($id),
            __('message.company.request_approved')
        );
    }

    /**
     * 회사 추가 신청 반려
     */
    public function reject(CompanyRequestActionRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->rejectRequest($id, $request->validated()['reason'] ?? null),
            __('message.company.request_rejected')
        );
    }

    /**
     * 내 회사 추가 신청 목록
     */
    public function myRequests(CompanyRequestIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->companyService->getMyRequests($request->validated()),
            __('message.fetched')
        );
    }
}
