<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\LiquidRenderer;
use App\Models\DistVideo ;
use App\Models\DistVideoCategory ;
class VideoController extends Controller
{
    protected $liquidRenderer;

    public function __construct(LiquidRenderer $liquidRenderer)
    {
        $this->liquidRenderer = $liquidRenderer;
    }

    /**
     * Display a listing of the videos.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $videos = DistVideo::paginate(10)->where('dist_id', getDistId()); // 每页显示10个视频
        return $this->liquidRenderer->render('videos.index', ['videos' => $videos]);
    }

    public function category($slug)
    {


        // 获取分类信息
        $category = DistVideoCategory::where(function ($query) use ($slug) {
            $query->where('id', $slug)
                ->orWhere('slug', $slug);
        })
            ->where('dist_id', getDistId())
            ->firstOrFail();

        // 获取分类下的所有产品，并按 is_pinned 排序，然后进行分页
        $videos = DistVideo::where('category_id', $category->id)
            ->where('dist_id', getDistId())
            ->where('enabled', 1)
            ->orderBy('order', 'desc')
            ->orderBy('id', 'desc')
            ->paginate(12);


        // 创建分页数据结构
        $paginator = [
            'previous_page' => $videos->previousPageUrl() ? true : false, // 是否有上一页
            'previous_page_url' => $videos->previousPageUrl(), // 上一页的 URL
            'next_page' => $videos->nextPageUrl() ? true : false, // 是否有下一页
            'next_page_url' => $videos->nextPageUrl(), // 下一页的 URL
            'current_page' => $videos->currentPage(), // 当前页
            'total_pages' => $videos->lastPage(), // 总页数
            'pages' => range(1, $videos->lastPage()), // 页码数组
            'page_url' => array_combine(
                range(1, $videos->lastPage()),
                array_map(fn($page) => $videos->url($page), range(1, $videos->lastPage()))
            ), // 每页的 URL
        ];

        // 构建导航数据 开始
        $breadcrumbs = [
            [
                'url' => '/',
                'name' => 'Home',
            ]
        ];

        $breadcrumbs[] = [
            'url' => '#',
            'name' => 'Video',
        ];
        $breadcrumbs[] = [
            'url' => '#',
            'name' => $category->name,
        ];

        // 渲染模板并传递数据
        return $this->liquidRenderer->render('videos_categories.liquid', [
            'category' => $category, // 分类名称
            'videos' => $videos,           // 分类下的产品
            'paginator' => $paginator, // 分页信息
            'breadcrumbs' => $breadcrumbs,
        ]);
    }



    public function detail($slug)
    {

        // 获取视频信息，并确保 dist_id 符合 getDistId() 的要求
        $video = DistVideo::where('dist_id', getDistId())
            ->where(function ($query) use ($slug) {
                $query->where('slug', $slug)
                    ->orWhere('id', $slug);
            })
            ->firstOrFail();

        // 构建导航数据 开始
        $breadcrumbs = [
            [
                'url' => '/',
                'name' => 'Home',
            ]
        ];

        $category=$video->distVideoCategory;

        $breadcrumbs[] = [
            'url' => '#',
            'name' => $category->name,
        ];

        $breadcrumbs[] = [
            'url' => '#',
            'name' => $video->title,
        ];


        return $this->liquidRenderer->render('videos_detail.liquid',
            [
                'video' => $video,
               // 'csrf_token' => csrf_token(),
                'breadcrumbs' => $breadcrumbs,
            ]);
    }
}
