<?php

namespace App\Services;

use App\Models\Article;
use App\Models\Category;
use App\Models\Event;

class MagazineService
{
    /**
     * Get Categories
     * @return mixed
     */
    public function getCategories()
    {
        // number of items can be (article or events) in each category
        $categories = Category::withCount([
            'articles' => function ($query) {
                $query->full()->where('status', 'published');
            }
        ])->get();

        return $categories;
    }

    /**
     * Get category by slug
     *
     * @param string $slug
     * @return Category
     */
    public function getCategoryBySlug(string $slug)
    {
        $category = Category::full()
            ->where('slug', $slug)
            ->where('status', 'published')
            ->first();

        return $category;
    }

    /**
     * Get articles
     *
     * @param array $params
     * @return Paginator
     */
    public function getArticles(array $params)
    {
        $itemsPerPage = $params['itemsPerPage'] ?? 9;
        $page = $params['page'] ?? 1;

        $query = Article::full()->where('status', 'published');

        if (array_key_exists('categoryId', $params)) {
            $query->where('category_id', $params['categoryId']);
        }

        $articles = $query->orderBy('id', 'desc')
            ->paginate($itemsPerPage, ['*'], 'page', $page);

        return $articles;
    }

    /**
     * Get article by slug
     *
     * @param string $slug
     * @return array | Article
     */
    public function getArticleBySlug(string $slug)
    {
        $article = Article::full()
            ->where('slug', $slug)
            ->where('status', 'published')
            ->first();

        if ($article) {
            $articleRelated = Article::full()
                ->where('status', 'published')
                ->where('category_id', $article->category_id)
                ->where('id', '!=', $article->id)
                ->orderBy('id', 'desc')
                ->take(3)
                ->get();

            return [
                'data' => $article,
                'related' => $articleRelated
            ];
        }

        return $article;
    }

    /**
     * Get events
     *
     * @param array $params
     * @return Paginator
     */
    public function getEvents(array $params)
    {
        $itemsPerPage = $params['itemsPerPage'] ?? 9;
        $page = $params['page'] ?? 1;

        $query = Event::full()->where('status', 'published');
        /* It was decided that events have only one special category and they are all part of it
         *
        if (array_key_exists('categoryId', $params)) {
            $query->where('category_id', $params['categoryId']);
        }
        */

        $events = $query->orderBy('id', 'desc')
            ->paginate($itemsPerPage, ['*'], 'page', $page);

        return $events;
    }

    /**
     * Get event by slug
     *
     * @param string $slug
     * @return Event
     */
    public function getEventBySlug(string $slug)
    {
        $event = Event::full()->where('slug', $slug)->first();

        return $event;
    }
}
