<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\DTO\SearchData;

class CommentBlame extends Model
{
    protected $table = 'tb_comment_blame';
    protected $primaryKey = 'id';

    public $keyType = 'int';
    public $incrementing = true;
    public $timestamps = true;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = null;

    protected $guarded = [];

    public function board()
    {
        return $this->belongsTo(Board::class)->withDefault();
    }

    public function post()
    {
        return $this->belongsTo(Post::class)->withDefault();
    }

    public function user()
    {
        return $this->belongsTo(User::class)->withDefault();
    }

    /**
     * 댓글 신고 목록
     */
    public function data(SearchData $params): object
    {
        $query = $this->query();
        $query->select(
            'tb_board.name AS boardName',
            'tb_board.code',
            'tb_post.subject',
            'tb_comment_blame.*',
            'user_1.sid AS sid_1',
            'user_1.name AS name_1',
            'user_2.sid AS sid_2',
            'user_2.name AS name_2'
        );

        if($params->keyword) {
            switch ($params->field) {
                case 'tb_comment_blame.ip_address' :
                case 'user_1.name' :
                case 'user_1.email' :
                case 'user_2.name' :
                case 'user_2.email' :
                    $query->where($params->field, 'LIKE', "%{$params->keyword}%");
                    break;
                case 'tb_comment_blame.id' :
                case 'tb_comment_blame.post_id' :
                case 'user_1.id' :
                case 'user_1.sid' :
                case 'user_2.id' :
                case 'user_2.sid' :
                    $query->where($params->field, '=', $params->keyword);
                    break;
            }
        }

        if($params->startDate || $params->endDate) {
            if($params->startDate) {
                $query->where('tb_comment_blame.created_at', '>=', $params->startDate . ' 00:00:00');
            }
            if($params->endDate) {
                $query->where('tb_comment_blame.created_at', '<=', $params->endDate . ' 23:59:59');
            }
        }

        if($params->boardID) {
            $query->where('tb_comment_blame.board_id', '=', $params->boardID);
        }

        $query->join('tb_board', 'tb_board.id', '=', 'tb_comment_blame.board_id'); // 게시판
        $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_blame.post_id'); // 게시글
        $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_blame.comment_id'); // 댓글
        $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_comment.user_id'); // 작성자
        $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_comment_blame.user_id'); // 신고자
        $query->orderByDesc('tb_comment_blame.id');

        $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
        $total = $this->count();
        $rows = $list->count();

        return (object)[
            'total' => $total,
            'rows' => $rows,
            'list' => $list
        ];
    }

    /**
     * 댓글 신고 접수
     */
    public function register(array $params): int
    {
        return $this->insertGetId($params);
    }

    /**
     * 댓글 신고 결과 변경
     */
    public function commentBlameResultUpdate(int $commentBlameID, int $status, string $memo): int
    {
        return $this->find($commentBlameID)->update([
            'status' => $status,
            'memo' => $memo
        ]);
    }

    /**
     * 댓글 신고 존재 유무
     */
    public function isAlready(Comment $comment, int $userID): bool
    {
        return $this->where([
            ['post_id', $comment->post_id],
            ['comment_id', $comment->id],
            ['user_id', $userID]
        ])->exists();
    }
}
