<?php

namespace App\Http\Controllers\Board;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\BoardRequest;
use App\Services\BoardService;
use App\Models\Board;
use App\Models\BoardMeta;

class BoardController extends Controller
{
    protected BoardService $boardService;

    protected string $code; // 게시판 코드
    protected Board $board; // 게시판 정보
    protected BoardMeta $boardMeta; // 게시판 설정 값

    public function __construct(Request $request, BoardService $boardService)
    {
        $this->middleware(['front', 'authed']);

        // 게시글 접근 권한 확인
        $this->middleware('board.access')->only('index');

        $this->boardService = $boardService;

        $this->code = $request->route('code');
    }

    /**
     * 공지사항 / 게시글 목록 또는 검색 결과
     * @method GET
     * @see /board/{code}
     */
    public function index(BoardRequest $request)
    {
        $this->board = $request->board;
        $this->boardMeta = $request->boardMeta;
        $boardID = $this->board->getKey();

        // 분류
        $categories = $this->boardService->categories($boardID);

        // 공지사항
        $notices = $this->boardService->notices($request);

		// 일반 게시글
		$posts = $this->boardService->posts($request);

        $listURL = route('board.list', $this->code);
        $writeURL = route('board.post.write', $this->code);

        return view(layout('board.index'), [
            'board' => $this->board,
            'boardMeta' => $this->boardMeta,
            'categories' => $categories,
            'notices' => $notices,
            'posts' => $posts,
            'params' => $request,
            'listURL' => $listURL,
            'writeURL' => $writeURL
        ]);
    }
}
