<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Card\StoreCardRequest;
use App\Http\Requests\V1\Card\UpdateCardRequest;
use App\Services\CardService;
use Illuminate\Http\Request;

class CardController extends Controller
{
    public function __construct(
        private readonly CardService $service
    ) {}

    /**
     * 카드 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'status',
            'assigned_user_id',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 카드 등록
     */
    public function store(StoreCardRequest $request)
    {
        $card = $this->service->store($request->validated());

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

    /**
     * 카드 상세
     */
    public function show(int $id)
    {
        $card = $this->service->show($id);

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

    /**
     * 카드 수정
     */
    public function update(int $id, UpdateCardRequest $request)
    {
        $card = $this->service->update($id, $request->validated());

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

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

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

    /**
     * 카드 상태 토글 (사용/정지)
     */
    public function toggle(int $id)
    {
        $card = $this->service->toggleStatus($id);

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

    /**
     * 활성 카드 목록 (셀렉트박스용)
     */
    public function active()
    {
        $cards = $this->service->getActiveCards();

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