<?php

namespace App\Http\Controllers;

use App\Article;
use App\FeatureDetail;
use App\Http\Resources\FeatureResource;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use App\Feature;

class FeatureController extends Controller
{
    const ITEM_PER_PAGE = 50;

    public function index(Request $request)
    {
        $params = $request->all();
        $limit = Arr::get($params, 'limit', static::ITEM_PER_PAGE);
        $page = Arr::get($params, 'page', 1);

        $list = Cache::remember('feature_index_' . $page, (7 * 24 * 60 * 60), function () use ($limit) {
            $items = Feature::select('id', 'name', 'slug', 'title', 'description', 'image', 'created_at')
                ->isPublished()
                ->orderBy('id', 'DESC')
                ->paginate($limit);
            return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

        return FeatureResource::collection($list['items'])->response()->header('Last-Modified', $list['lastModify']);
    }

    public function hot(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);

        $list = Cache::remember('feature_hot_' . $limit, (7 * 24 * 60 * 60), function () use ($limit) {
            $items = Feature::select('id', 'name', 'slug', 'title', 'description', 'image', 'created_at')
                ->isPublished()
                ->orderBy('id', 'DESC')
                ->limit($limit)
                ->get();
            return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

        return response()->json(['data' => $list['items']], 200)->header('Last-Modified', $list['lastModify']);
    }

    public function show($slug = '')
    {
        $slug = trim($slug);
        $key = str_replace("-", "_", $slug);
        $list = Cache::remember('feature_show_' . $key, (7 * 24 * 60 * 60), function () use ($slug) {
            $item = Feature::select('id', 'name', 'slug', 'title', 'description', 'image', 'created_at')
                ->isPublished()
                ->where('slug', $slug)
                ->first();
            return ['item' => $item, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

        $articles = Cache::remember('feature_show_article_' . $key, (7 * 24 * 60 * 60), function () use ($slug) {
            $featureItem = Feature::select('id')->isPublished()->where('slug', $slug)->first();
            if (!isset($featureItem)) return [];
            $listDetailIds = FeatureDetail::select('article_id')->where('feature_id', $featureItem->id)->pluck('article_id')->toArray();
            return Article::select('id', 'title', 'slug', 'thumbnail', 'publish_at')
                ->isPublished()
                ->whereIn('id', $listDetailIds)
                ->orderBy('updated_at', 'DESC')
                ->orderBy('important', 'DESC')
                ->limit(30)
                ->get();
        });

        $relates = Cache::remember('feature_show_relate_' . $key . '', (7 * 24 * 60 * 60), function () use ($slug) {
            return Feature::select('id', 'name', 'slug', 'title', 'description', 'image', 'created_at')
                ->isPublished()
                ->where('slug', '!=', $slug)
                ->orderBy('updated_at', 'DESC')
                ->limit(20)
                ->get();
        });

        return response()->json([
            'data'      => $list['item'],
            'article'   => $articles,
            'relate'    => $relates,
        ], 200)->header('Last-Modified', $list['lastModify']);
    }
}
