<?php

namespace App\Http\Controllers\Wap\User;

use App\Modules\User\Services\AnnuallyMonthlyUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\Book\Services\CrmBookAutoRecommendService;
use Hashids;
use DB;

class CrmBooklistController extends Controller
{    
    public function index(Request $request)
    {
        $month = date('n');
        $this->statsDetail($this->getUserColumnValue('id'),'entrance');
        return view('crm.entrance')->with(compact('month'));
    }

    public function bookshow(Request $request)
    {
        $booklists = (new CrmBookAutoRecommendService)->getRecommendBooksFromRedis();
        $book_models = collect($booklists)->groupBy('category_id')
            ->map(function ($item, $key) {
                $category = collect($item)->first();
                $category_id = $category['category_id'];
                $category_name = $category['category_name'];
                $books = collect($item)->sortByDesc('recommend_index')->values()->transform(function ($item) {
                    $item['star'] =  intval(($item['recommend_index'] - 50) / 10);
                    $item['link'] = sprintf(
                        '%s://site%s.%s.com/detail?id=%s&crm=%s',
                        env('PROTOCOL'),
                        encodeDistributionChannelId($this->getUserColumnValue('channel_id')),
                        env('CUSTOM_HOST'),
                        Hashids::encode($item['bid']),
                        'crm_book_show_'.$item['bid']
                    );
                    return $item;
                })->take(4)->all();
                return compact('category_id', 'category_name', 'books');
            })->all();
        $annually_monthly_user = new AnnuallyMonthlyUser();
        $is_annually_monthly_user = $annually_monthly_user->is_monthly || $annually_monthly_user->is_annually;
        $this->statsDetail($this->getUserColumnValue('id'),'bookshow');
        return view('crm.bookshow')->with(compact('book_models','is_annually_monthly_user'));
    }

    public function booklist(Request $request)
    {
        $month = date('n');
        $category_id = $request->get('category_id', 0);
        $books = [];
        if ($category_id) {
            $booklists = (new CrmBookAutoRecommendService)->getRecommendBooksFromRedis();
            $books =  collect($booklists)->where('category_id', $category_id)->transform(function ($item) {
                $item['link'] = sprintf(
                    '%s://site%s.%s.com/detail?id=%s&crm=%s',
                    env('PROTOCOL'),
                    encodeDistributionChannelId($this->getUserColumnValue('channel_id')),
                    env('CUSTOM_HOST'),
                    Hashids::encode($item['bid']),
                    'crm_book_list_'.$item['bid']
                );
                return $item;
            })->all();
        }
        $this->statsDetail($this->getUserColumnValue('id'),'booklist');
        return view('crm.booklist', compact('books', 'month')); #->with(compact('books', 'month'));
    }

    private function getUserColumnValue(string $column)
    {
        $user = app()->make('user');
        if ($user) {
            return $user->$column;
        }
    }


    private function statsDetail($uid,$page,$bid=0)
    {
        try{
            DB::table('crm_visit_detail')->insert([
                'uid'=>$uid,'page'=>$page,
                'bid'=>$bid,'day'=>date('Y-m-d'),
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
        }catch (\Exception $e){}

    }
}
