<?php

namespace App\Http\Controllers;

use App\Services\LiquidRenderer;
use Illuminate\Http\Request;
use App\Models\SitePage;
use App\Models\SitePageTag;

class CollectionController extends Controller
{

    protected $liquidRenderer;

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

    public function index()
    {

        //模板支持多级目录，需要目录符号
        $output = LiquidRenderer::render('collection_list.liquid', [

        ]);

        return response($output);
    }

    public function detail($slug)
    {

        // Find the tag by either slug or id
        $tag = SitePageTag::where('dist_id', getDistId())
            ->where(function ($query) use ($slug) {
                $query->where('slug', $slug)
                    ->orWhere('id', $slug);
            })
            ->first();

        if (!$tag) {
            return response()->json(['message' => '标签未找到'], 404);
        }


        // 获取关联的 pages
        $pages = $tag->pages()->get();


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


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

        // 构建导航数据 结束

        //模板支持多级目录，需要目录符号
        $output = LiquidRenderer::render('collection_list.liquid', [
            'tag' => $tag,
            'pages' => $pages,
            'breadcrumbs' => $breadcrumbs,
        ]);

        return response($output);
    }


}
