<?php
namespace App\Controllers;

use App\Models\CustomerCase;

/**
 * 前台「客户案例」：列表 + 详情，与新闻前台同构。
 */
class CaseController extends Controller
{
    public function index()
    {
        $case = new CustomerCase();
        $seo = page_seo('cases', [
            'title'       => '客户案例',
            'description' => '酷冰甲降温服客户案例展示，覆盖消防、电力、钢铁、环卫、户外施工、车间制造等高温作业场景的真实合作项目，逐一呈现降温方案设计思路、现场使用效果与客户真实反馈，并附上适用行业与选型建议，为同类企业的高温防护升级提供可参考、可复用的实战样本，切实降低高温作业风险。',
            'keywords'    => '降温服案例,客户案例,高温作业,降温方案,消防降温,工业应用,酷冰甲案例',
            'og_type'     => 'website',
        ]);
        return $this->view('cases/index', [
            'pageSeo' => [
                'title'       => $seo['title'],
                'description' => $seo['description'],
                'keywords'    => $seo['keywords'],
                'og_type'     => $seo['og_type'] ?: 'website',
                'og_image'    => $seo['og_image'],
                'canonical'   => $seo['canonical'],
                'noindex'     => $seo['noindex'],
                'breadcrumb'  => [
                    ['name' => '首页', 'url' => site_url()],
                    ['name' => '客户案例', 'url' => absolute_url()],
                ],
            ],
            'cases' => $case->published(20),
        ]);
    }

    public function show($slug)
    {
        $case = new CustomerCase();
        $c = $case->where('slug', $slug);
        if (!$c && is_numeric($slug)) { $c = $case->find((int)$slug); }
        if (!$c) { \Core\App::notFound(); return ''; }
        // 浏览量 +1
        $case->update($c['id'], ['views' => ($c['views'] ?? 0) + 1]);
        $all = $case->published(20);
        $idx = array_search($c, $all);
        $prev = $idx !== false && $idx > 0 ? $all[$idx - 1] : null;
        $next = $idx !== false && $idx < count($all) - 1 ? $all[$idx + 1] : null;

        $cTitle = e($c['title'] ?? '案例详情');
        $cSummary = mb_substr(strip_tags($c['summary'] ?? $c['body'] ?? ''), 0, 160);
        $cImage = $c['image'] ?? '';
        $publishedAt = $c['created_at'] ?? $c['published_at'] ?? date('Y-m-d');

        // ── Article JSON-LD Schema（客户案例）────
        $articleSchema = '<script type="application/ld+json">' . json_encode([
            '@context'      => 'https://schema.org',
            '@type'         => 'Article',
            'headline'      => $c['title'] ?? '',
            'description'   => $cSummary,
            'image'         => $cImage,
            'datePublished' => $publishedAt,
            'dateModified'  => $c['updated_at'] ?? $publishedAt,
            'author'        => ['@type' => 'Organization', 'name' => '酷冰甲'],
        ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>';

        return $this->view('cases/show', [
            'pageSeo' => [
                'title'       => $cTitle,
                'description' => $cSummary,
                'og_type'     => 'article',
                'og_image'    => $cImage,
                'breadcrumb'  => [
                    ['name' => '首页', 'url' => site_url()],
                    ['name' => '客户案例', 'url' => site_url('cases')],
                    ['name' => $c['title'] ?? '案例', 'url' => absolute_url()],
                ],
                'jsonld'      => $articleSchema,
            ],
            'c'    => $c,
            'prev' => $prev,
            'next' => $next,
        ]);
    }
}
