<?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\Post;
use App\Models\PostBlame;
use App\Models\DTO\SearchData;
use Exception;

class PostController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private PostBlame $postBlameModel;

    public function __construct(Board $board, PostBlame $postBlame)
    {
        $this->boardModel = $board;
        $this->postBlameModel = $postBlame;
    }

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

        $blameData = $this->postBlameModel->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.post.edit', $row->id);
                $row->typeStr = MAP_BLAME_TYPE[$row->type];
                $row->reason = ($row->reason ?? '-');

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

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

    /**
     * 게시판 > 신고 관리 삭제
     * @method DELETE
     * @see /admin/board/blame/destroy
     */
    public function destroy(Request $request, Post $postModel)
    {
        try {

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

            if ($chk) {
                foreach ($chk as $postBlameID) {
                    $postBlame = $this->postBlameModel->findOrNew($postBlameID);

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

                    if(!$postBlame->delete()) {
                        throw new Exception($postBlameID . "번 신고글을 삭제할 수 없습니다.");
                    }

                    // 신고 수 감소
                    $postModel->decreaseBlame($postBlame->post_id);

                }
            }

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

    /**
     * 게시판 > 신고 상세 보기
     * @method GET
     * @see /admin/board/blame/post/{pk}/edit
     */
    public function edit(int $postBlameID)
    {
        $blame = $this->postBlameModel->findOrFail($postBlameID);
        $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.post.edit', [
            'blame' => $blame
        ]);
    }
}
