<?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',
            'search',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

        // 거래처 기준 응답으로 변환
        $transformedData = $clients->through(function ($client) {
            // 가장 최근 활성 악성채권 정보
            $latestBadDebt = $client->activeBadDebts->first();

            return [
                'id' => $client->id,
                'client_id' => $client->id,
                'client_code' => $client->client_code,
                'client_name' => $client->name,
                'business_no' => $client->business_no,
                'contact_person' => $client->contact_person,
                'phone' => $client->phone,
                'mobile' => $client->mobile,
                'email' => $client->email,
                'address' => $client->address,
                'client_type' => $client->client_type,
                // 집계 데이터
                'total_debt_amount' => (float) ($client->total_debt_amount ?? 0),
                'max_overdue_days' => (int) ($client->max_overdue_days ?? 0),
                'bad_debt_count' => (int) ($client->active_bad_debt_count ?? 0),
                // 대표 상태 (가장 최근 악성채권의 상태)
                'status' => $latestBadDebt?->status ?? 'collecting',
                'is_active' => true, // 리스트에 나온 건 모두 활성
                // 담당자 (가장 최근 악성채권의 담당자)
                'assigned_user' => $latestBadDebt?->assignedUser ? [
                    'id' => $latestBadDebt->assignedUser->id,
                    'name' => $latestBadDebt->assignedUser->name,
                ] : null,
                // 개별 악성채권 목록
                'bad_debts' => $client->activeBadDebts->map(fn ($bd) => [
                    'id' => $bd->id,
                    'debt_amount' => (float) $bd->debt_amount,
                    'status' => $bd->status,
                    'overdue_days' => $bd->overdue_days,
                    'is_active' => $bd->is_active,
                    'occurred_at' => $bd->occurred_at?->toDateString(),
                    'assigned_user' => $bd->assignedUser ? [
                        'id' => $bd->assignedUser->id,
                        'name' => $bd->assignedUser->name,
                    ] : null,
                ])->toArray(),
            ];
        });

        return ApiResponse::success($transformedData, __('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'));
    }
}
