<?php

namespace App\Models\Movie;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class MovieLike extends Model
{
    protected $table = 'tb_movie_like';
    protected $primaryKey = 'id';

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

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

    protected $guarded = [];

    public function getType(int $reviewID, int $userID): MovieLike
    {
        return $this->where([
            ['movie_review_id', $reviewID],
            ['user_id', $userID]
        ])->firstOrNew();
    }

    public function isExists(int $reviewID, int $userID): int
    {
        $sql = "
            SELECT IF(COUNT(*) > 0, 1, 0) AS `exists` FROM tb_movie_like WHERE movie_review_id = ? AND user_id = ?;
        ";

        return DB::selectOne($sql, [$reviewID, $userID])->exists;
    }

    public function isLike(int $reviewID, int $userID): bool
    {
        return $this->where([
            ['movie_review_id', $reviewID],
            ['type', 1],
            ['user_id', $userID]
        ])->exists();
    }

    public function isDisLike(int $reviewID, int $userID): bool
    {
        return $this->where([
            ['movie_review_id', $reviewID],
            ['type', 2],
            ['user_id', $userID]
        ])->exists();
    }
}
