<?php

namespace Modules\Cms\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\Cms\Http\Resources\ArticleCollection;
use Modules\Cms\Http\Resources\ArticleResource;
use Modules\Cms\Models\Article;

class ArticleController extends Controller
{

    /**
     * Notes   : 文章列表
     *
     * @Date   : 2021/4/16 10:45 上午
     * @Author : < Jason.C >
     * @param  Request  $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $categoryId = $request->category_id;
        $title      = $request->title;
        $subTitle   = $request->sub_title;
        $slug       = $request->slug;
        $tagId      = $request->tag_id;

        $result = Article::query()
            ->when($title, function ($query) use ($title) {
                $query->where('title', 'like', "%{$title}%");
            })
            ->when($subTitle, function ($query) use ($subTitle) {
                $query->where('sub_title', 'like', "%{$subTitle}%");
            })
            ->when($slug, function ($query) use ($slug) {
                $query->where('slug', 'like', "%{$slug}%");
            })
            ->when($categoryId, function ($query) use ($categoryId) {
                $query->whereHas('categories', function ($cate) use ($categoryId) {
                    $cate->where('category_id', $categoryId);
                });
            })
            ->when($tagId, function ($query) use ($tagId) {
                $query->whereHas('tags', function ($tag) use ($tagId) {
                    $tag->where('tag_id', $tagId);
                });
            })
            ->where('status', 1)
            ->with(['categories', 'tags'])
            ->paginate(10);

        return $this->success(new ArticleCollection($result));
    }

    /**
     * Notes   : 文章详情
     *
     * @Date   : 2021/4/16 10:45 上午
     * @Author : < Jason.C >
     * @param  Article  $article
     * @return mixed
     */
    public function show(Article $article)
    {
        $article->read(Api::userId());

        return $this->success(new ArticleResource($article));
    }

    /**
     * Notes: 收藏/取消收藏
     *
     * @Author: 玄尘
     * @Date: 2022/10/11 9:59
     */
    public function favorite(Article $article): JsonResponse
    {
        $user = Api::user();

        $user->toggleFavorite($article);

        return $this->success([
            'count'    => $article->favorites()->count(),
            'favorite' => $user->hasFavorited($article),
        ]);
    }

    /**
     * Notes: 点赞/取消点赞
     *
     * @Author: 玄尘
     * @Date: 2022/10/11 10:18
     * @param  Article  $article
     * @return JsonResponse
     * @throws \Exception
     */
    public function subscribe(Article $article): JsonResponse
    {
        $user = Api::user();

        $user->toggleSubscribe($article);

        return $this->success([
            'count'      => $article->subscribers()->count(),
            'subscribed' => $user->hasSubscribed($article),
        ]);
    }

}