<?php

namespace App\Http\Controllers\Board;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Models\DTO\SearchData;
use App\Services\TagService;

/**
 * 2026.01.07 작성
 */
class TagController extends Controller
{
    private TagService $tagService;

    public function __construct(TagService $tagService) 
    {
        $this->middleware('front');

        $this->tagService = $tagService;
    }

    /**
     * 태그 보기
     * @method GET
     * @see /tags
     */
    public function index(Request $request)
    {
        $tags = $this->tagService->getAllTags();

        return view(layout('board.tag'), [
            'tags' => $tags,
        ]);
    }

    /**
     * 태그에 연결된 게시글 조회
     * @method GET
     * @see /tag/posts?={tag}
     */
    public function posts(Request $request): JsonResponse
    {
        $params = SearchData::fromRequest($request);
        $data = $this->tagService->getPostsByTag($params);

        return response()->json([
            'data' => $data,
        ]);
    }
}
