<?php

namespace App\Http\Controllers\Admin\Board\Board;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Board;
use App\Models\BoardMeta;
use App\Models\BoardCategory;

class CategoryController extends Controller
{
    private Board $boardModel;
    private BoardMeta $boardMetaModel;
    private BoardCategory $boardCategoryModel;

    public function __construct(Board $board, BoardMeta $boardMeta, BoardCategory $boardCategory)
    {
        $this->boardModel = $board;
        $this->boardMetaModel = $boardMeta;
        $this->boardCategoryModel = $boardCategory;
    }

    /**
     * 게시판 수정 - 분류
     * @method GET
     * @see /admin/board/board/category/{pk}
     */
    public function show(int $boardID)
    {
        return view('admin.board.board.category', [
            'boardID' => $boardID,

            // 게시판 분류
            'boardCategoryData' => $this->boardCategoryModel->getCategoryList($boardID),

            // 게시판 정보
            'boardData' => $this->boardModel->find($boardID),

            // 게시판 메타 정보
            'boardMetaData' => $this->boardMetaModel->getAllMeta($boardID),

            // 모든 게시판
            'boardAllData' => $this->boardModel->all()
        ]);
    }

    /**
     * 게시판 그룹 분류 등록 저장
     * @method POST
     * @see /admin/board/board/category/{pk}
     */
    public function store(Request $request)
    {
        $rules = [
            'board_id' => 'required|numeric|exists:tb_board,id',
            'name' => 'required|string',
            'depth' => 'required|numeric|min:0',
        ];

        $attributes = [
            'board_id' => '게시판 수정 - 분류',
            'category_id' => '분류 PK',
            'parent_id' => '분류 부모 PK',
            'name' => '분류 명',
            'depth' => '분류 깊이',
        ];

        $type = $request->post('type');
        if($type == 'modify') { // 수정
            $rules['board_category_id'] = 'required|numeric|exists:tb_board_category,id';
            $rules['parent_id'] = 'numeric|nullable|exists:tb_board_category,parent_id';
        }else{ // 입력
            $rules['parent_id'] = 'required|numeric|min:0';
        }

        $posts = $this->validate($request, $rules, [], $attributes);
        $boardID = $posts['board_id'];
        $parentID = ($posts['parent_id'] ?? null);
        $today = now();

        $saveData = [
            'board_id' => $boardID,
            'parent_id' => $parentID,
            'name' => $posts['name'],
            'depth' => $posts['depth']
        ];

        if($type == 'add') {
            $code = $this->boardCategoryModel->nextKey($parentID, $boardID);
            $saveData['code'] = $code;
            $saveData['created_at'] = $today;
            $this->boardCategoryModel->register($saveData);
            $message = '게시판 분류가 새로 등록되었습니다.';
        }else{
            $saveData['id'] = $posts['board_category_id'];
            $saveData['updated_at'] = $today;
            $this->boardCategoryModel->updater($saveData);
            $message = '게시판 분류가 수정 되었습니다.';
        }

        return redirect()->route('admin.board.board.category.show', $boardID)->with('message', $message);
    }

    /**
     * 게시판 그룹 분류 삭제
     * @method DELETE
     * @see /admin/board/board/category/{pk}/destroy
     */
    public function destroy(Request $request)
    {
        $boardID = $request->post('board_id');
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $boardCategoryID) {
                $this->boardCategoryModel->remove($boardCategoryID);
            }
        }

        $message = '게시판 분류 정보가 삭제되었습니다.';
        return redirect()->route('admin.board.board.category.show', $boardID)->with('message', $message);
    }

    /**
     * 분류 한 단계 위로
     */
    public function up(int $boardCategoryID)
    {
        $boardID = $this->boardCategoryModel->find($boardCategoryID)->getOriginal('board_id');
        if($boardID) {
            $this->boardCategoryModel->orderUp($boardCategoryID);
            return redirect()->route('admin.board.board.category.show', $boardID);
        }else{
            return back();
        }
    }

    /**
     * 분류 한 단계 아래로
     */
    public function down(int $boardCategoryID)
    {
        $boardID = $this->boardCategoryModel->find($boardCategoryID)->getOriginal('board_id');
        if($boardID) {
            $this->boardCategoryModel->orderDown($boardCategoryID);
            return redirect()->route('admin.board.board.category.show', $boardID);
        }else{
            return back();
        }
    }
}
