<?php

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

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Traits\AgentTrait;
use App\Models\Board;
use App\Models\Comment;
use App\Models\CommentBlame;
use App\Models\DTO\SearchData;
use Exception;

class CommentController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private CommentBlame $commentBlameModel;

    public function __construct(Board $board, CommentBlame $commentBlame)
    {
        $this->boardModel = $board;
        $this->commentBlameModel = $commentBlame;
    }

    /**
     * 댓글 > 신고 관리 조회
     * @method GET
     * @see /admin/board/blame/comment
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $params->boardID = $request->post('board_id');

        $blameData = $this->commentBlameModel->data($params);

        if ($blameData->rows > 0) {
            $num = listNum($blameData->total, $params->page, $params->perPage);
            foreach ($blameData->list as $i => $row) {
                $row->num = $num--;
                $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
                $row->editURL = route('admin.board.blame.comment.edit', $row->id);
                $row->typeStr = MAP_BLAME_TYPE[$row->type];
                $row->reason = ($row->reason ?? '-');

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

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

    /**
     * 댓글 > 신고 관리 삭제
     * @method DELETE
     * @see /admin/board/blame/comment/destroy
     */
    public function destroy(Request $request, Comment $commentModel)
    {
        try {

            $chk = $request->post('chk');

            if ($chk) {
                foreach ($chk as $commentBlameID) {
                    $commentBlame = $this->commentBlameModel->findOrNew($commentBlameID);

                    if (!$commentBlame->exists) {
                        throw new Exception($commentBlameID . '번 신고글은 존재하지 않습니다.');
                    }

                    if (!$commentBlame->delete()) {
                        return back()->withErrors($commentBlameID . "번 신고글을 삭제할 수 없습니다.")->withInput();
                    }

                    // 신고 수 감소
                    $commentModel->decreaseBlame($commentBlameID);
                }
            }

            $message = '신고 기록이 삭제되었습니다.';
            return redirect()->route('admin.board.comment.blame.index')->with('message', $message);
        } catch (Exception $e) {
            return back()->withErrors($e->getMessage())->withInput();
        }
    }

    /**
     * 댓글 > 신고 상세 보기
     * @method GET
     * @see /admin/board/blame/comment/{pk}/edit
     */
    public function edit(int $commentBlameID)
    {
        $blame = $this->commentBlameModel->findOrFail($commentBlameID);
        $blame->device = $this->device($blame->user_agent);
        $blame->platform = $this->platform($blame->user_agent);
        $blame->browser = $this->browser($blame->user_agent);

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