<?php
namespace App\Controllers;

use App\Models\News;

class NewsController extends Controller
{
    public function index()
    {
        $news = new News();
        $seo = page_seo('news', [
            'title'       => '新闻动态',
            'description' => '酷冰甲降温服行业新闻中心，汇集高温防护政策解读、降温技术深度解析、产品应用案例、客户现场实录与行业前沿动态，持续分享降温服选型、使用、保养与清洗知识，帮助企业做好高温作业人员的健康与安全防护。我们关注每一次技术迭代，也记录每一处真实应用，让高温防护更有依据、更可落地。',
            'keywords'    => '降温服新闻,降温技术,高温防护,工业降温,降温服应用,行业动态,酷冰甲资讯,降温服知识',
            'og_type'     => 'website',
        ]);
        return $this->view('news/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()],
                ],
            ],
            'news' => $news->published(20),
        ]);
    }

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

        $nTitle = e($n['title'] ?? '文章详情');
        $nSummary = mb_substr(strip_tags($n['summary'] ?? $n['body'] ?? ''), 0, 160);
        $nImage = $n['image'] ?? '';
        $publishedAt = $n['created_at'] ?? $n['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'      => $n['title'] ?? '',
            'description'   => $nSummary,
            'image'         => $nImage,
            'datePublished' => $publishedAt,
            'dateModified'  => $n['updated_at'] ?? $publishedAt,
            'author'        => ['@type' => 'Organization', 'name' => '酷冰甲'],
        ] + ($prev ? ['mainEntityOfPage' => ['@type' => 'WebPage', '@id' => absolute_url()]] : []),
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>';

        return $this->view('news/show', [
            'pageSeo' => [
                'title'       => $nTitle,
                'description' => $nSummary,
                'og_type'     => 'article',
                'og_image'    => $nImage,
                'breadcrumb'  => [
                    ['name' => '首页', 'url' => site_url()],
                    ['name' => '新闻动态', 'url' => site_url('news')],
                    ['name' => $n['title'] ?? '文章', 'url' => absolute_url()],
                ],
                'jsonld'      => $articleSchema,
            ],
            'n'    => $n,
            'prev' => $prev,
            'next' => $next,
        ]);
    }
}
