<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\AccountSubject\StoreAccountSubjectRequest;
use App\Services\AccountCodeService;
use Illuminate\Http\Request;

class AccountSubjectController extends Controller
{
    public function __construct(
        private readonly AccountCodeService $service
    ) {}

    /**
     * 계정과목 목록 조회
     */
    public function index(Request $request)
    {
        $params = $request->only(['search', 'category']);

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

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

    /**
     * 계정과목 등록
     */
    public function store(StoreAccountSubjectRequest $request)
    {
        $subject = $this->service->store($request->validated());

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

    /**
     * 계정과목 활성/비활성 토글
     */
    public function toggleStatus(int $id, Request $request)
    {
        $isActive = (bool) $request->input('is_active', true);

        $subject = $this->service->toggleStatus($id, $isActive);

        return ApiResponse::success($subject, __('message.toggled'));
    }

    /**
     * 계정과목 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

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