<?php

namespace App\Http\Controllers;

use App\Http\Resources\FortunemethodResource;
use App\Fortunemethod;
use App\Indexer\MethodIndex;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use DB;

class FortuneMethodController extends Controller
{
    const ITEM_PER_PAGE = 20;

    public function index()
    {
        $list = Fortunemethod::select('id', 'name', 'slug')
            ->isPublished()
            ->orderBy('order')
            ->orderBy('id')
            ->limit(static::ITEM_PER_PAGE)
            ->get();

        return FortunemethodResource::collection($list);
    }

    public function getBySlug($slug = '')
    {
        $method = Fortunemethod::select('id', 'name', 'slug')->isPublished()->where('slug', $slug)->first();
        if (!isset($method)) return response()->json(['errors' => 'Method is not valid'], 403);
        return response()->json(['data' => $method], 200);
    }

    public function all()
    {
    	$list = Fortunemethod::select('id', 'name', 'slug', 'logo', 'top_name')
            ->isPublished()
            ->orderBy('order')
            ->orderBy('id')
            ->get();

        return response()->json(['data' => $list], 200);
    }

    public function selection(Request $request)
    {
        $params = $request->all();
        $limit = Arr::get($params, 'limit', static::ITEM_PER_PAGE);
        $list = Cache::remember('method_selection_' . $limit, (7*24*60*60), function () use ($limit) {
            $items = MethodIndex::select('id', 'name', 'slug')
                ->orderBy('name')
                ->paginate($limit);

            return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

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

        /*$list = Fortunemethod::select('id', 'name', 'slug')
        ->isPublished()
        ->orderBy('order')
        ->orderBy('id')
        ->paginate($limit);*/
    }

    public function topMost(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', 30);
        $keyword = Arr::get($searchParams, 'keyword', "");
        $list = [];

        if ($keyword != "") {
            $list['items'] = Fortunemethod::select('id', 'name', 'slug', 'logo', 'top_name')
                ->where('is_deleted', false)
                ->where('name', 'like', "%$keyword%")
                ->orderBy('id', 'DESC')
                ->paginate($limit);
            $list['lastModify'] = (gmdate('D, d M Y H:i:s') . ' GMT');
        } else {
            $list = Cache::remember('method_topMost_' . $limit, (7*24*60*60), function () use ($limit) {
                $items = Fortunemethod::select('fortune_methods.id', 'fortune_methods.name', 'fortune_methods.slug', 'fortune_methods.logo', 'fortune_methods.top_name', 'fortune_method_details.fortune_method_id', DB::raw('COUNT(fortune_methods.id) as total'))
                    ->leftJoin('fortune_method_details', 'fortune_methods.id', '=', 'fortune_method_details.fortune_method_id')
                    ->where('fortune_methods.is_deleted', false)
                    ->groupBy('fortune_methods.id')
                    ->orderByRaw('COUNT(fortune_methods.id) desc')
                    ->orderBy('fortune_methods.id', 'DESC')
                    ->paginate($limit);

                return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
            });
        }

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

    public function top(Request $request)
    {
    	$searchParams = $request->all();
    	$limit = Arr::get($searchParams, 'limit', 6);
        $list = Cache::remember('method_top_' . $limit, (7*24*60*60), function () use ($limit) {
            $items = Fortunemethod::select('id', 'name', 'slug', 'logo', 'top_name')
                ->isPublished()
                ->where('show_top', true)
                ->orderBy('order')
                ->orderBy('id')
                ->limit($limit)
                ->get();

            return ['items' => $items, 'lastModify' => (gmdate('D, d M Y H:i:s') . ' GMT')];
        });

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