<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\BadDebt\StoreBadDebtDocumentRequest;
use App\Http\Requests\V1\BadDebt\StoreBadDebtMemoRequest;
use App\Http\Requests\V1\BadDebt\StoreBadDebtRequest;
use App\Http\Requests\V1\BadDebt\UpdateBadDebtRequest;
use App\Services\BadDebtService;
use Illuminate\Http\Request;

class BadDebtController extends Controller
{
    public function __construct(
        private readonly BadDebtService $service
    ) {}

    /**
     * 악성채권 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'client_id',
            'status',
            'is_active',
            'search',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 악성채권 요약 통계
     */
    public function summary(Request $request)
    {
        $params = $request->only(['client_id']);

        $summary = $this->service->summary($params);

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

    /**
     * 악성채권 등록
     */
    public function store(StoreBadDebtRequest $request)
    {
        $badDebt = $this->service->store($request->validated());

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

    /**
     * 악성채권 상세
     */
    public function show(int $id)
    {
        $badDebt = $this->service->show($id);

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

    /**
     * 악성채권 수정
     */
    public function update(int $id, UpdateBadDebtRequest $request)
    {
        $badDebt = $this->service->update($id, $request->validated());

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

    /**
     * 악성채권 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

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

    /**
     * 설정 토글 (is_active)
     */
    public function toggle(int $id)
    {
        $badDebt = $this->service->toggle($id);

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

    /**
     * 서류 첨부
     */
    public function addDocument(int $id, StoreBadDebtDocumentRequest $request)
    {
        $document = $this->service->addDocument($id, $request->validated());

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

    /**
     * 서류 삭제
     */
    public function removeDocument(int $id, int $documentId)
    {
        $this->service->removeDocument($id, $documentId);

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

    /**
     * 메모 추가
     */
    public function addMemo(int $id, StoreBadDebtMemoRequest $request)
    {
        $memo = $this->service->addMemo($id, $request->validated());

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

    /**
     * 메모 삭제
     */
    public function removeMemo(int $id, int $memoId)
    {
        $this->service->removeMemo($id, $memoId);

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