<?php

namespace App\Models;

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

class PostBlame extends Model
{
    protected $table = 'tb_post_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_post_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_post_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_post_blame.id' :
                case 'tb_post_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_post_blame.created_at', '>=', $params->startDate . ' 00:00:00');
            }
            if($params->endDate) {
                $query->where('tb_post_blame.created_at', '<=', $params->endDate . ' 23:59:59');
            }
        }

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

        $query->join('tb_board', 'tb_board.id', '=', 'tb_post_blame.board_id'); // 게시판
        $query->join('tb_post', 'tb_post.id', '=', 'tb_post_blame.post_id'); // 게시글
        $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_post.user_id'); // 작성자
        $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_post_blame.user_id'); // 신고자
        $query->orderByDesc('tb_post_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 postBlameResultUpdate(int $postBlameID, int $status, string $memo): int
    {
        return $this->find($postBlameID)->update([
            'status' => $status,
            'memo' => $memo
        ]);
    }

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