<?php

namespace App\Http\Controllers\V1\User;

use App\Http\Controllers\V1\Controller;
use App\Models\EpisodesList;
use App\Models\UserFavorite;

class FavoriteController extends Controller
{
    // 检查当前短剧是否喜欢
    public function check($id)
    {
        $episode = EpisodesList::find($id);
        $res     = UserFavorite::filterUser()->where('list_id', $id)->count();
        $number  = UserFavorite::where('list_id', $id)->count();
        $number += $episode->episode->favorite_num;
        $data = [
            'isFav'  => !empty($res),
            'number' => $number,
        ];

        return $this->success($data);
    }

    // 添加喜欢
    public function add($id)
    {
        $episode = EpisodesList::find($id);
        $res     = UserFavorite::filterUser()->where('list_id', $id)->count();
        if ($res) {
            return $this->success(true);
        }
        $collect             = new UserFavorite();
        $collect->user_id    = \user()->id;
        $collect->episode_id = $episode->episodes_id;
        $collect->list_id    = $id;

        return $this->success($collect->save());
    }

    // 删除喜欢
    public function destroy($id)
    {
        $res = UserFavorite::filterUser()->where('list_id', $id)->first();
        if (!$res) {
            return $this->success(false);
        }

        return $this->success($res->delete());
    }
}
