<?php

namespace App\Models;

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

class CommentLike extends Model
{
    protected $table = 'tb_comment_like';
    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);
    }

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

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

    public function comment()
    {
        return $this->belongsTo(Comment::class);
    }

    /**
     * 댓글 추천/비추 삭제
     */
    public function data(SearchData $params): object
    {
        $query = $this->query();
        $query->select(
            'tb_board.name AS boardName',
            'tb_board.code',
            'tb_post.subject',
            'tb_comment.content',
            'tb_comment_like.*',
            '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_like.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_like.id' :
                case 'tb_comment_like.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('created_at', '>=', $params->startDate . ' 00:00:00');
            }
            if($params->endDate) {
                $query->where('created_at', '<=', $params->endDate . ' 23:59:59');
            }
        }

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

        $query->join('tb_board', 'tb_board.id', '=', 'tb_comment_like.board_id');
        $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_like.post_id');
        $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_like.comment_id');
        $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_comment_like.user_id'); // 추천인
        $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_comment.user_id'); // 작성자
        $query->orderByDesc('tb_comment_like.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 info(Comment $comment, int $userID): CommentLike
    {
        return $this->where([
            ['post_id', $comment->post_id],
            ['comment_id', $comment->id],
            ['user_id', $userID]
        ])->firstOrNew();
    }

    /**
     * 댓글 좋아요/싫어요 등록
     */
    public function register(array $params): int
    {
        return $this->insertGetId($params);
    }

    /**
     * 댓글 좋아요/싫어요 삭제
     */
    public function remove(Comment $comment, int $userID): int
    {
        return $this->where([
            ['post_id', $comment->post_id],
            ['comment_id', $comment->id],
            ['user_id', $userID]
        ])->delete();
    }

    /**
     * 회원이 좋아요/싫어요 둘중 어느것 선택했는지 확인
     */
    public function isAlready(Comment $comment, ?int $userID, int $type): bool
    {
        return $this->where([
            ['post_id', $comment->post_id],
            ['comment_id', $comment->id],
            ['user_id', $userID],
            ['type', $type]
        ])->exists();
    }
}
