<?php

namespace Modules\ContentManage\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Catch\Exceptions\FailedException;
use Modules\ContentManage\Models\Book;
use Modules\ContentManage\Models\BookCategories;
use Modules\ContentManage\Models\BookChapterContents;
use Modules\ContentManage\Models\BookChapters;
use Modules\ContentManage\Models\Output\OutputChannel;
use Modules\ContentManage\Models\Output\OutputMapping;

/**
 * 书籍输出
 */
final class OutputController extends Controller{

    private $output_channel_id = 0;
    private $channel_name = '';
    public function __construct(protected readonly Book $book,
    protected readonly OutputChannel $outputChannel,
    protected readonly OutputMapping $outputMapping,
    protected readonly BookChapters $bookChapters,
    protected readonly BookChapterContents $bookChapterContents,
    protected readonly BookCategories $bookCategories,
    Request $request)
    {
        $this->checkAccess($request);
    }


    /**
     * 渠道参数检验
     *
     * @param Request $request
     * @return void
     */
    private function checkAccess(Request $request){
        $channel_name = $request->get('channel_name');
        $channel_key = $request->get('channel_key');
        if(empty($channel_name) || empty($channel_key)){
            throw new FailedException("缺少参数",11001);
        }

        $output_channel_info = $this->outputChannel->active()
        ->where('channel_name',$channel_name)->select('id','api_key')->first();
        if(!$output_channel_info || $output_channel_info->api_key != $channel_key){
            throw new FailedException("渠道参数错误",11002);
        }
        $this->output_channel_id = $output_channel_info->id;
        $this->channel_name = $channel_name;
        return ;

    }

    /**
     * bid校验
     *
     * @param [type] $bid
     * @return void
     */
    private  function checkBid($bid){
        if(!is_numeric($bid)){
            throw new FailedException("参数错误",11003);
        }

        if($this->channel_name == 'zhuishuyun'){
            return true;
        }

        $map_exists = $this->outputMapping->active()->where('bid',$bid)->count();
        if(!$map_exists){
            throw new FailedException("书籍不存在",11004);
        }
    }

    /**
     * 书籍列表
     *
     * @return void
     */
    public final function bookList(Request $request){
        $cp_name = $request->get('cp_name');
        if($cp_name ){
            return $this->outputMapping->active()
            ->join('books','books.id','=','book_output_mappings.bid')
            ->where('books.cp_name',$cp_name)
            ->where('output_channel_id',$this->output_channel_id)
            ->select('book_output_mappings.bid','books.name as book_name')->get();
        }
        return $this->outputMapping->active()
        ->join('books','books.id','=','book_output_mappings.bid')
        ->where('output_channel_id',$this->output_channel_id)
        ->where('chapter_count','>',0)
        ->select('book_output_mappings.bid','books.name as book_name')->get();
    }

    /**
     * 书籍详情
     *
     * @param integer $bid
     * @return void
     */
    public final function bookDetail($bid){
        $this->checkBid($bid);
        return $this->book->firstBy($bid,'id',[
            'id as bid',
        'name as book_name','author','intro as Introduction','cover','status','channel as channelid','cp_name as cp',
        'keyword','size as booklength','category_name','category_id','updated_at','end_date as copyright_end_date'
        ]);
    }

   /**
    * 章节列表
    *
    * @param integer $bid
    * @return void
    */
    public final function chapterList($bid){
        $this->checkBid($bid);
        return $this->bookChapters->where('bid',$bid)
        ->select('id as chapter_id','bid','name as chapter_name','sequence','is_vip','size','updated_at')
        ->get();
    }

    /**
     * 章节内容
     *
     * @param integer $bid
     * @param integer $chapter_id
     * @return void
     */
    public final function chapterContent($bid,$chapter_id){
        if(!is_numeric($chapter_id)){
            throw new FailedException("参数错误",11003);
        }
        $this->checkBid($bid);
        $chapter_info = $this->bookChapters->firstBy($chapter_id,'id',['bid','chapter_content_id','name']);
        if(!$chapter_info || $chapter_info->bid != $bid || !$chapter_info->chapter_content_id){
            throw new FailedException("章节不存在",11004);
        }

        $content_info = $this->bookChapterContents->firstBy($chapter_info->chapter_content_id,'id',['content']);
        return [
            'chapter_id'=>$chapter_id,
            'chapter_name'=>$chapter_info->name,
            'chapter_content'=>$content_info->content
        ];
    }


    public final function listCategories(){
        $category_list = $this->bookCategories->show()
        ->select('id','category_name','channel_id','channel_name','pid')->get();
        $result = [
            ['channel_id'=>1,'channel_name'=>'男频','list'=>[]],
            ['channel_id'=>2,'channel_name'=>'女频','list'=>[]],
        ];
        foreach ($category_list as $item){
            //if($item->pid == 0) continue;
            $temp = ['category_id'=>$item->id,'category_name'=>$item->category_name];
            if($item->channel_id == 1){
                $result[0]['list'][] = $temp;
            }
            if($item->channel_id == 2){
                $result[1]['list'][] = $temp;
            }
        }
        return $result;
    }


 
}