<?php

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

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Traits\AgentTrait;
use App\Models\Board;
use App\Models\CommentDeleted;
use App\Models\DTO\SearchData;

class CommentController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private CommentDeleted $commentDeletedModel;

    public function __construct(Board $board, CommentDeleted $commentDeleted)
    {
        $this->boardModel = $board;
        $this->commentDeletedModel = $commentDeleted;
    }

    /**
     * 게시판 > 휴지통 관리(댓글)
     * @method GET
     * @see /admin/board/trash/comment
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $params->boardID = $request->get('board_id');

        $commentDeletedData = $this->commentDeletedModel->data($params);

        if ($commentDeletedData->rows > 0) {
            $num = listNum($commentDeletedData->total, $params->page, $params->perPage);
            foreach ($commentDeletedData->list as $i => $row) {
                $row->num = $num--;
                $row->editURL = route('admin.board.trash.comment.edit', $row->id);

                $commentDeletedData->list[$i] = $row;
            }
        }

        return view('admin.board.trash.comment.index', [
            'commentDeletedData' => $commentDeletedData,
            'params' => $params,
            'boardData' => $this->boardModel->all()
        ]);
    }

    /**
     * 게시판 > 휴지통 관리(댓글) > 전체삭제
     * @method GET
     * @see /admin/board/trash/comment/truncate
     */
    public function truncate()
    {
        if(!$this->commentDeletedModel->truncate()) {
            return back()->withErrors("휴지통 전체 삭제에 실패하였습니다.")->withInput();
        }

        $message = '휴지통에 댓글을 전체 삭제하였습니다.';
        return redirect()->route('admin.board.trash.comment.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(댓글) > 선택삭제
     * @method GET
     * @see /admin/board/trash/comment/destroy
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $deletedID) {
                if(!$this->commentDeletedModel->remove($deletedID)) {
                    return back()->withErrors($deletedID . "번 댓글을 삭제할 수 없습니다.")->withInput();
                }
            }
        }

        $message = '댓글이 삭제되었습니다.';
        return redirect()->route('admin.board.trash.comment.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(댓글) > 복원하기
     * @method POST
     * @see /admin/board/trash/comment/recovery
     */
    public function recovery(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $deletedID) {
                if(!$this->commentDeletedModel->recovery($deletedID)) {
                    return back()->withErrors($deletedID . "번 댓글을 복원할 수 없습니다.")->withInput();
                }
            }
        }

        $message = '댓글이 복원되었습니다.';
        return redirect()->route('admin.board.trash.comment.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(댓글) > 상세보기
     * @method GET
     * @see /admin/board/trash/comment/{pk}/edit
     */
    public function edit(int $deletedID)
    {
        $deleted = $this->commentDeletedModel->findOrFail($deletedID);
        $deleted->device = $this->device($deleted->user_agent);
        $deleted->platform = $this->platform($deleted->user_agent);
        $deleted->browser = $this->browser($deleted->user_agent);

        return view('admin.board.trash.comment.edit', [
            'deleted' => $deleted
        ]);
    }
}
