<?php

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

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

class PostController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private PostLike $postLikeModel;

    public function __construct(Board $board, PostLike $postLike)
    {
        $this->boardModel = $board;
        $this->postLikeModel = $postLike;
    }

    /**
     * 게시판 > 추천/비추 조회
     * @method GET
     * @see /admin/board/like/post
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $params->boardID = $request->post('board_id');

        $likeData = $this->postLikeModel->data($params);

        if ($likeData->rows > 0) {
            $num = listNum($likeData->total, $params->page, $params->perPage);
            foreach ($likeData->list as $i => $row) {
                $row->num = $num--;
                $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
                $row->editURL = route('admin.board.like.post.edit', $row->id);

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

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

    /**
     * 게시판 > 추천/비추 삭제
     * @method DELETE
     * @see /admin/board/like/post/destroy
     */
    public function destroy(Request $request, Post $postModel)
    {
        try {

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

            if ($chk) {
                foreach ($chk as $postLikeID) {
                    $postLike = $this->postLikeModel->findOrNew($postLikeID);

                    if(!$postLike->exists) {
                        throw new Exception($postLikeID . '번 추천/비추 기록이 존재하지 않습니다.');
                    }

                    if(!$postLike->delete()) {
                        throw new Exception($postLikeID . '번 추천/비추 기록을 삭제할 수 없습니다.');
                    }

                    // 게시글 추천/비추천 개수 갱신
                    if($postLike->type == 1) {
                        $postModel->decreaseLike($postLike->post_id);
                    }else{
                        $postModel->decreaseDisLike($postLike->post_id);
                    }
                }
            }

            $message = '게시글 추천/비추 기록이 삭제되었습니다.';
            return redirect()->route('admin.board.like.post.index')->with('message', $message);
        } catch (Exception $e) {
            return back()->withErrors($e->getMessage())->withInput();
        }
    }

    /**
     * 게시판 > 추천/비추천 상세 보기
     * @method GET
     * @see /admin/board/like/{pk}/edit
     */
    public function edit(int $postLikeID)
    {
        $like = $this->postLikeModel->findOrFail($postLikeID);
        $like->device = $this->device($like->user_agent);
        $like->platform = $this->platform($like->user_agent);
        $like->browser = $this->browser($like->user_agent);

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