<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\BulkUpdateAccountCodeRequest;
use App\Http\Requests\V1\Deposit\StoreDepositRequest;
use App\Http\Requests\V1\Deposit\UpdateDepositRequest;
use App\Services\DepositService;
use Illuminate\Http\Request;

class DepositController extends Controller
{
    public function __construct(
        private readonly DepositService $service
    ) {}

    /**
     * 입금 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'start_date',
            'end_date',
            'client_id',
            'payment_method',
            'bank_account_id',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 입금 등록
     */
    public function store(StoreDepositRequest $request)
    {
        $deposit = $this->service->store($request->validated());

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

    /**
     * 입금 상세
     */
    public function show(int $id)
    {
        $deposit = $this->service->show($id);

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

    /**
     * 입금 수정
     */
    public function update(int $id, UpdateDepositRequest $request)
    {
        $deposit = $this->service->update($id, $request->validated());

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

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

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

    /**
     * 입금 요약 (기간별 합계)
     */
    public function summary(Request $request)
    {
        $params = $request->only([
            'start_date',
            'end_date',
            'client_id',
            'payment_method',
        ]);

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

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

    /**
     * 계정과목 일괄 변경
     */
    public function bulkUpdateAccountCode(BulkUpdateAccountCodeRequest $request)
    {
        $updatedCount = $this->service->bulkUpdateAccountCode(
            $request->getIds(),
            $request->getAccountCode()
        );

        return ApiResponse::success(
            ['updated_count' => $updatedCount],
            __('message.bulk_updated')
        );
    }
}
