<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Loan\LoanCalculateInterestRequest;
use App\Http\Requests\Loan\LoanIndexRequest;
use App\Http\Requests\Loan\LoanSettleRequest;
use App\Http\Requests\Loan\LoanStoreRequest;
use App\Http\Requests\Loan\LoanUpdateRequest;
use App\Services\LoanService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LoanController extends Controller
{
    public function __construct(
        private readonly LoanService $loanService
    ) {}

    /**
     * 가지급금 목록
     */
    public function index(LoanIndexRequest $request): JsonResponse
    {
        $result = $this->loanService->index($request->validated());

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

    /**
     * 가지급금 요약
     */
    public function summary(LoanIndexRequest $request): JsonResponse
    {
        $validated = $request->validated();
        $userId = $validated['user_id'] ?? null;
        $category = $validated['category'] ?? null;
        $result = $this->loanService->summary($userId, $category);

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

    /**
     * 가지급금 대시보드
     */
    public function dashboard(Request $request): JsonResponse
    {
        $startDate = $request->query('start_date');
        $endDate = $request->query('end_date');

        $result = $this->loanService->dashboard($startDate, $endDate);

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

    /**
     * 가지급금 등록
     */
    public function store(LoanStoreRequest $request): JsonResponse
    {
        $result = $this->loanService->store($request->validated());

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

    /**
     * 가지급금 상세
     */
    public function show(int $id): JsonResponse
    {
        $result = $this->loanService->show($id);

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

    /**
     * 가지급금 수정
     */
    public function update(LoanUpdateRequest $request, int $id): JsonResponse
    {
        $result = $this->loanService->update($id, $request->validated());

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

    /**
     * 가지급금 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        $this->loanService->destroy($id);

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

    /**
     * 가지급금 정산
     */
    public function settle(LoanSettleRequest $request, int $id): JsonResponse
    {
        $result = $this->loanService->settle($id, $request->validated());

        return ApiResponse::success($result, __('message.loan.settled'));
    }

    /**
     * 인정이자 계산
     */
    public function calculateInterest(LoanCalculateInterestRequest $request): JsonResponse
    {
        $validated = $request->validated();
        $result = $this->loanService->calculateInterest(
            $validated['year'],
            $validated['user_id'] ?? null
        );

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

    /**
     * 인정이자 리포트
     */
    public function interestReport(int $year): JsonResponse
    {
        $result = $this->loanService->interestReport($year);

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

    /**
     * 세금 시뮬레이션
     */
    public function taxSimulation(LoanCalculateInterestRequest $request): JsonResponse
    {
        $validated = $request->validated();
        $result = $this->loanService->taxSimulation($validated['year']);

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