<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\GeneralJournalEntry\StoreManualJournalRequest;
use App\Http\Requests\V1\GeneralJournalEntry\UpdateJournalRequest;
use App\Services\GeneralJournalEntryService;
use Illuminate\Http\Request;

class GeneralJournalEntryController extends Controller
{
    public function __construct(
        private readonly GeneralJournalEntryService $service
    ) {}

    /**
     * 일반전표 통합 목록 조회
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'start_date', 'end_date', 'search', 'page', 'per_page',
        ]);

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

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

    /**
     * 요약 통계
     */
    public function summary(Request $request)
    {
        $params = $request->only([
            'start_date', 'end_date', 'search',
        ]);

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

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

    /**
     * 수기전표 등록
     */
    public function store(StoreManualJournalRequest $request)
    {
        $entry = $this->service->store($request->validated());

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

    /**
     * 전표 상세 조회 (분개 수정 모달용)
     */
    public function show(int $id)
    {
        $detail = $this->service->show($id);

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

    /**
     * 분개 수정
     */
    public function updateJournal(int $id, UpdateJournalRequest $request)
    {
        $entry = $this->service->updateJournal($id, $request->validated());

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

    /**
     * 분개 삭제
     */
    public function destroyJournal(int $id)
    {
        $this->service->destroyJournal($id);

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