<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SiteAdminBusinessPagesTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsSiteAdmin(): void
    {
        $this->seed();

        $this->post('/site-admin/login', [
            'email' => 'merchant.admin@demo.local',
            'password' => 'Merchant@123456',
        ])->assertRedirect('/site-admin');
    }

    public function test_site_dashboard_displays_site_scope_content(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin')
            ->assertOk()
            ->assertSee('站点后台仪表盘')
            ->assertSee('站点管理员')
            ->assertSee('站点用户')
            ->assertSee('站点商品')
            ->assertSee('站点订单');
    }

    public function test_site_business_pages_display_expected_entries(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/merchants')
            ->assertOk()
            ->assertSee('站点商家')
            ->assertSee('筛选条件')
            ->assertSee('导出当前筛选结果 CSV')
            ->assertSee('当前筛选摘要')
            ->assertSee('站点运营摘要')
            ->assertSee('运营关注项')
            ->assertSee('工作台导航')
            ->assertSee('商品工作台')
            ->assertSee('订单工作台')
            ->assertSee('站点信息')
            ->assertSee('当前站点资料')
            ->assertSee('站点承接说明')
            ->assertSee('运营建议')
            ->assertSee('查看站点商品')
            ->assertSee('查看站点订单')
            ->assertSee('查看低库存商品')
            ->assertSee('查看待支付订单')
            ->assertSee('高库存已上架商品')
            ->assertSee('低库存补货视角')
            ->assertSee('最近新增商品')
            ->assertSee('高金额已支付订单')
            ->assertSee('待支付订单跟进')
            ->assertSee('支付失败订单排查')
            ->assertSee('查看当前站点资料')
            ->assertSee('查看承接记录')
            ->assertSee('返回当前筛选视图')
            ->assertSee('建议动作')
            ->assertSee('去看最近新增商品')
            ->assertSee('去看已上架商品')
            ->assertSee('去看待支付订单')
            ->assertSee('去看支付失败订单')
            ->assertSee('去看站点资料')
            ->assertSee('去看承接记录')
            ->assertSee('当前站点承接记录')
            ->assertSee('查看商品')
            ->assertSee('查看订单')
            ->assertSee('低库存')
            ->assertSee('支付失败')
            ->assertSee('快捷入口默认带入更适合运营查看的排序与状态条件')
            ->assertSee('/site-admin/products?sort=stock_desc&amp;status=published', false)
            ->assertSee('/site-admin/products?sort=stock_asc&amp;status=published', false)
            ->assertSee('/site-admin/products?sort=latest', false)
            ->assertSee('/site-admin/orders?sort=pay_amount_desc&amp;payment_status=paid', false)
            ->assertSee('/site-admin/orders?sort=latest&amp;payment_status=unpaid', false)
            ->assertSee('/site-admin/orders?sort=latest&amp;payment_status=failed', false)
            ->assertSee('#site-profile', false)
            ->assertSee('#site-records', false);

        $this->get('/site-admin/products')
            ->assertOk()
            ->assertSee('站点商品')
            ->assertSee('筛选条件')
            ->assertSee('商品运营汇总')
            ->assertSee('当前筛选摘要')
            ->assertSee('运营关注项')
            ->assertSee('工作台导航')
            ->assertSee('当前信号')
            ->assertSee('建议动作')
            ->assertSee('商品状态统计')
            ->assertSee('最低价格')
            ->assertSee('最高库存')
            ->assertSee('导出当前筛选结果 CSV')
            ->assertSee('商品列表');

        $this->get('/site-admin/orders')
            ->assertOk()
            ->assertSee('站点订单')
            ->assertSee('筛选条件')
            ->assertSee('订单汇总')
            ->assertSee('当前筛选摘要')
            ->assertSee('运营关注项')
            ->assertSee('工作台导航')
            ->assertSee('当前信号')
            ->assertSee('建议动作')
            ->assertSee('订单状态统计')
            ->assertSee('最低实付金额')
            ->assertSee('最高实付金额')
            ->assertSee('导出当前筛选结果 CSV')
            ->assertSee('订单列表');
    }

    public function test_site_merchant_page_can_reflect_current_filters_and_stats(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/merchants?keyword=%E6%BC%94%E7%A4%BA&status=active&plan=pro&sort=name_asc')
            ->assertOk()
            ->assertSee('演示店铺')
            ->assertSee('启用中')
            ->assertSee('专业版')
            ->assertSee('名称 A-Z')
            ->assertSee('站点管理员数')
            ->assertSee('站点商品数')
            ->assertSee('站点订单数')
            ->assertSee('商品分类数')
            ->assertSee('运营关注项')
            ->assertSee('工作台导航')
            ->assertSee('站点承接说明')
            ->assertSee('运营建议')
            ->assertSee('当前站点商品仍较少，建议优先查看最近新增与基础信息是否完整。')
            ->assertSee('当前站点订单已形成基础规模，建议优先关注待支付、支付失败与高金额订单。')
            ->assertSee('联系人信息已具备，可继续作为站点日常跟进入口。')
            ->assertSee('建议动作')
            ->assertSee('去看最近新增商品')
            ->assertSee('去看已上架商品')
            ->assertSee('去看待支付订单')
            ->assertSee('去看支付失败订单')
            ->assertSee('去看站点资料')
            ->assertSee('去看承接记录')
            ->assertSee('/site-admin/products?sort=stock_desc&amp;status=published', false)
            ->assertSee('/site-admin/products?sort=stock_asc&amp;status=published', false)
            ->assertSee('/site-admin/orders?sort=pay_amount_desc&amp;payment_status=paid', false)
            ->assertSee('/site-admin/orders?sort=latest&amp;payment_status=failed', false)
            ->assertSee('#site-profile', false)
            ->assertSee('#site-records', false);
    }

    public function test_site_merchant_export_contains_scope_summary_and_filtered_content(): void
    {
        $this->loginAsSiteAdmin();

        $export = $this->get('/site-admin/merchants/export?keyword=%E6%BC%94%E7%A4%BA&status=active&plan=pro&sort=name_asc');
        $export->assertOk();
        $content = $export->streamedContent();

        $this->assertStringContainsString('导出信息,站点商家导出', $content);
        $this->assertStringContainsString('站点ID,1', $content);
        $this->assertStringContainsString('站点名称,演示店铺', $content);
        $this->assertStringContainsString('关键词,演示', $content);
        $this->assertStringContainsString('状态,启用中', $content);
        $this->assertStringContainsString('套餐,专业版', $content);
        $this->assertStringContainsString('排序', $content);
        $this->assertStringContainsString('名称 A-Z', $content);
        $this->assertStringContainsString('站点管理员数,1', $content);
        $this->assertStringContainsString('站点商品数,2', $content);
        $this->assertStringContainsString('站点订单数,5', $content);
        $this->assertStringContainsString('当前站点资料,', $content);
        $this->assertStringContainsString('当前套餐,专业版', $content);
        $this->assertStringContainsString('ID,名称,Slug,状态,套餐,联系人,联系电话,联系邮箱,管理员数,用户数,商品数,订单数,商品分类数,激活时间', $content);
        $this->assertStringContainsString('演示店铺,demo-shop,启用中,专业版', $content);
    }

    public function test_site_product_filters_can_narrow_results(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?keyword=%E6%BC%94%E7%A4%BA%E5%95%86%E5%93%81&status=published')
            ->assertOk()
            ->assertSee('演示商品')
            ->assertDontSee('精选礼包');
    }

    public function test_site_product_range_filters_can_narrow_results(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?min_price=150&max_price=220')
            ->assertOk()
            ->assertSee('演示商品')
            ->assertDontSee('精选礼包');

        $this->get('/site-admin/products?min_stock=60')
            ->assertOk()
            ->assertSee('演示商品')
            ->assertDontSee('精选礼包');
    }

    public function test_site_product_invalid_ranges_show_chinese_errors_and_empty_state(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?min_price=300&max_price=100&min_stock=abc')
            ->assertOk()
            ->assertSee('最低价格不能大于最高价格。')
            ->assertSee('最低库存必须为整数。')
            ->assertSee('暂无商品');
    }

    public function test_site_product_filter_summary_can_reflect_current_filters(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?keyword=%E6%BC%94%E7%A4%BA%E5%95%86%E5%93%81&status=published&category_id=1&min_price=150&max_price=220&min_stock=50&max_stock=120&sort=price_desc')
            ->assertOk()
            ->assertSee('当前筛选摘要')
            ->assertSee('演示商品')
            ->assertSee('已上架')
            ->assertSee('默认分类')
            ->assertSee('¥150.00 ~ ¥220.00')
            ->assertSee('50 ~ 120 件')
            ->assertSee('价格从高到低');
    }

    public function test_site_product_status_cards_can_drive_filtering(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?status=published')
            ->assertOk()
            ->assertSee('已上架')
            ->assertSee('演示商品')
            ->assertSee('精选礼包');

        $this->get('/site-admin/products?status=draft')
            ->assertOk()
            ->assertSee('暂无商品');
    }

    public function test_site_order_filters_can_narrow_results(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?status=shipped&platform=wechat_mini')
            ->assertOk()
            ->assertSee('ORD202603080003')
            ->assertDontSee('ORD202603080001');
    }

    public function test_site_order_pay_amount_range_filters_can_narrow_results(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?min_pay_amount=180&max_pay_amount=190')
            ->assertOk()
            ->assertSee('ORD202603080003')
            ->assertDontSee('ORD202603080002')
            ->assertDontSee('ORD202603080004');
    }

    public function test_site_order_invalid_ranges_show_chinese_errors_and_empty_state(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?min_pay_amount=300&max_pay_amount=100')
            ->assertOk()
            ->assertSee('最低实付金额不能大于最高实付金额。')
            ->assertSee('暂无订单');
    }

    public function test_site_order_filter_summary_can_reflect_current_filters(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?keyword=ORD202603080003&status=shipped&payment_status=paid&platform=wechat_mini&device_type=mini-program&payment_channel=wechat_pay&min_pay_amount=180&max_pay_amount=190&sort=pay_amount_desc')
            ->assertOk()
            ->assertSee('当前筛选摘要')
            ->assertSee('ORD202603080003')
            ->assertSee('已发货')
            ->assertSee('已支付')
            ->assertSee('微信小程序')
            ->assertSee('小程序环境')
            ->assertSee('微信支付')
            ->assertSee('¥180.00 ~ ¥190.00')
            ->assertSee('实付金额从高到低');
    }

    public function test_site_product_operations_focus_and_workbench_are_displayed(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products')
            ->assertOk()
            ->assertSee('当前站点商品仍较少，建议优先查看最近新增与已上架商品，确认基础信息是否完整。')
            ->assertSee('去看最近新增商品')
            ->assertSee('去看已上架商品')
            ->assertSee('已上架商品')
            ->assertSee('低库存商品')
            ->assertSee('分类覆盖数')
            ->assertSee('高库存已上架')
            ->assertSee('低库存补货')
            ->assertSee('最近新增')
            ->assertSee('草稿待整理')
            ->assertSee('返回当前筛选视图');
    }

    public function test_site_order_operations_focus_and_workbench_are_displayed(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders')
            ->assertOk()
            ->assertSee('当前站点订单已形成基础规模，建议优先关注待支付、支付失败与高金额已支付订单。')
            ->assertSee('去看待支付订单')
            ->assertSee('去看支付失败订单')
            ->assertSee('待支付订单')
            ->assertSee('支付失败订单')
            ->assertSee('已完成订单')
            ->assertSee('高金额已支付')
            ->assertSee('待支付跟进')
            ->assertSee('支付失败排查')
            ->assertSee('最近完成订单')
            ->assertSee('返回当前筛选视图');
    }

    public function test_site_product_operations_focus_can_follow_current_filters(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?status=published&min_stock=0&max_stock=20&sort=stock_asc')
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架库存视角，建议优先确认低库存补货节奏，并同步观察高库存结构是否健康。')
            ->assertSee('继续查看当前库存视角')
            ->assertSee('去看高库存商品')
            ->assertSee('/site-admin/products?status=published&amp;min_stock=0&amp;max_stock=20&amp;sort=stock_asc', false);

        $this->get('/site-admin/products?status=draft')
            ->assertOk()
            ->assertSee('当前正在查看草稿商品，建议优先补齐标题、分类、价格与库存后再安排上架。')
            ->assertSee('继续查看当前草稿')
            ->assertSee('去看已上架商品')
            ->assertSee('/site-admin/products?status=draft&amp;sort=latest', false);

        $response = $this->get('/site-admin/products?status=published&category_id=1&keyword=%E6%BC%94%E7%A4%BA&min_price=150&max_price=220&sort=price_desc');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架的“默认分类”分类下关键词“演示”命中的价格带 ¥150.00 ~ ¥220.00 商品，建议优先核对在售商品命名、分类承接、价格梯度与搜索结果是否一致，并同步观察库存结构是否健康。')
            ->assertSee('继续查看当前已上架分类关键词价格带商品')
            ->assertSee('去看当前已上架分类关键词商品')
            ->assertSee('status=published', false)
            ->assertSee('category_id=1', false)
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('min_price=150', false)
            ->assertSee('max_price=220', false)
            ->assertSee('sort=price_desc', false);

        $response = $this->get('/site-admin/products?status=published&category_id=1&keyword=%E6%BC%94%E7%A4%BA&sort=latest');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架的“默认分类”分类下关键词“演示”命中的商品，建议优先核对在售商品命名、分类承接与搜索结果是否一致，并同步观察价格带与库存结构是否健康。')
            ->assertSee('继续查看当前已上架分类关键词商品')
            ->assertSee('去看当前已上架分类商品')
            ->assertSee('status=published', false)
            ->assertSee('category_id=1', false)
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('sort=latest', false);

        $response = $this->get('/site-admin/products?status=published&category_id=1&min_price=150&max_price=220&sort=price_desc');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架的“默认分类”分类下价格带 ¥150.00 ~ ¥220.00 商品，建议优先核对该分类在售商品的价格结构、库存分布与承接效率是否协调。')
            ->assertSee('继续查看当前已上架分类价格带商品')
            ->assertSee('去看当前已上架分类商品')
            ->assertSee('status=published', false)
            ->assertSee('category_id=1', false)
            ->assertSee('min_price=150', false)
            ->assertSee('max_price=220', false)
            ->assertSee('sort=price_desc', false);

        $response = $this->get('/site-admin/products?status=published&keyword=%E6%BC%94%E7%A4%BA&min_price=150&max_price=220&sort=price_desc');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架商品中关键词“演示”命中的价格带 ¥150.00 ~ ¥220.00 结果，建议优先核对在售商品命名、卖点表达与价格梯度是否一致，并同步观察库存结构与搜索承接是否健康。')
            ->assertSee('继续查看当前已上架关键词价格带商品')
            ->assertSee('去看当前已上架关键词商品')
            ->assertSee('status=published', false)
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('min_price=150', false)
            ->assertSee('max_price=220', false)
            ->assertSee('sort=price_desc', false);

        $response = $this->get('/site-admin/products?status=published&keyword=%E6%BC%94%E7%A4%BA&sort=latest');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架商品中关键词“演示”命中的结果，建议优先核对在售商品命名、卖点表达与搜索承接是否一致，并同步观察价格带与库存结构是否健康。')
            ->assertSee('继续查看当前已上架关键词商品')
            ->assertSee('去看全部已上架商品')
            ->assertSee('status=published', false)
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('sort=latest', false);

        $response = $this->get('/site-admin/products?category_id=1&keyword=%E6%BC%94%E7%A4%BA&min_price=150&max_price=220&sort=price_desc');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦“默认分类”分类下关键词“演示”命中的价格带 ¥150.00 ~ ¥220.00 商品，建议优先核对分类承接、命名卖点与价格梯度是否一致，并同步观察库存结构与搜索承接是否健康。')
            ->assertSee('继续查看当前分类关键词价格带商品')
            ->assertSee('去看当前分类关键词商品')
            ->assertSee('category_id=1', false)
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('min_price=150', false)
            ->assertSee('max_price=220', false)
            ->assertSee('sort=price_desc', false);

        $response = $this->get('/site-admin/products?category_id=1&keyword=%E6%BC%94%E7%A4%BA&sort=latest');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦“默认分类”分类下关键词“演示”命中的商品，建议优先核对分类承接、命名卖点与搜索结果是否一致，并同步观察相关商品的价格带与库存结构。')
            ->assertSee('继续查看当前分类关键词商品')
            ->assertSee('去看当前分类商品')
            ->assertSee('keyword=%E6%BC%94%E7%A4%BA', false)
            ->assertSee('category_id=1', false)
            ->assertSee('sort=latest', false);

        $response = $this->get('/site-admin/products?status=published&category_id=1&sort=latest');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦已上架的“默认分类”分类商品，建议优先核对该分类在售商品的价格带、库存结构与承接质量是否均衡。')
            ->assertSee('继续查看当前已上架分类商品')
            ->assertSee('去看当前分类商品')
            ->assertSee('status=published', false)
            ->assertSee('category_id=1', false)
            ->assertSee('sort=latest', false);

        $response = $this->get('/site-admin/products?category_id=1&min_price=150&max_price=220&sort=price_desc');
        $response
            ->assertOk()
            ->assertSee('当前筛选已聚焦“默认分类”分类下价格带 ¥150.00 ~ ¥220.00 商品，建议优先核对该分类价格结构是否连贯，并同步观察库存分布与承接效率是否健康。')
            ->assertSee('继续查看当前分类价格带商品')
            ->assertSee('去看当前分类商品')
            ->assertSee('category_id=1', false)
            ->assertSee('min_price=150', false)
            ->assertSee('max_price=220', false)
            ->assertSee('sort=price_desc', false);

        $this->get('/site-admin/products?category_id=1&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦“默认分类”分类商品，建议优先核对分类承接是否准确，并同步观察价格带与库存结构是否均衡。')
            ->assertSee('继续查看当前分类商品')
            ->assertSee('去看低库存商品')
            ->assertSee('/site-admin/products?category_id=1&amp;sort=latest', false);

        $this->get('/site-admin/products?keyword=%E6%BC%94%E7%A4%BA&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦关键词“演示”命中的商品，建议优先核对命名、卖点与搜索承接是否一致，并同步观察相关商品的价格带与库存结构。')
            ->assertSee('继续查看当前关键词商品')
            ->assertSee('去看最近新增商品')
            ->assertSee('/site-admin/products?keyword=%E6%BC%94%E7%A4%BA&amp;sort=latest', false);

        $this->get('/site-admin/products?min_price=150&max_price=220&sort=price_desc')
            ->assertOk()
            ->assertSee('当前筛选已聚焦价格带 ¥150.00 ~ ¥220.00 商品，建议优先核对定价梯度是否连贯，并同步观察库存结构与转化表现是否匹配。')
            ->assertSee('继续查看当前价格带商品')
            ->assertSee('去看最近新增商品')
            ->assertSee('/site-admin/products?min_price=150&amp;max_price=220&amp;sort=price_desc', false);
    }

    public function test_site_order_operations_focus_can_follow_current_filters(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?platform=wechat_mini&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦微信小程序订单，建议优先关注下单到支付转化是否顺畅，并同步排查小程序端支付回流体验。')
            ->assertSee('继续查看微信小程序订单')
            ->assertSee('去看待支付订单')
            ->assertSee('/site-admin/orders?platform=wechat_mini&amp;sort=latest', false);

        $this->get('/site-admin/orders?payment_channel=wechat_pay&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦微信支付订单，建议优先核对支付成功率、回调稳定性与失败重试转化。')
            ->assertSee('继续查看微信支付订单')
            ->assertSee('去看支付失败订单')
            ->assertSee('/site-admin/orders?payment_channel=wechat_pay&amp;sort=latest', false);

        $this->get('/site-admin/orders?device_type=mini-program&payment_status=failed&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦小程序环境订单，建议优先核对授权链路、支付唤起表现与下单回流是否顺畅。')
            ->assertSee('继续查看小程序环境订单')
            ->assertSee('去看微信支付订单')
            ->assertSee('/site-admin/orders?payment_status=failed&amp;device_type=mini-program&amp;sort=latest', false);

        $this->get('/site-admin/orders?device_type=mobile-webview&payment_status=failed&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦微信内网页订单，建议优先关注授权静默登录、页面跳转稳定性与支付拉起后的回流体验。')
            ->assertSee('继续查看微信内网页订单')
            ->assertSee('去看待支付订单')
            ->assertSee('/site-admin/orders?payment_status=failed&amp;device_type=mobile-webview&amp;sort=latest', false);

        $this->get('/site-admin/orders?device_type=mobile&payment_status=failed&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦移动浏览器订单，建议优先关注 H5 下单链路、页面加载稳定性与支付转化流失点。')
            ->assertSee('继续查看移动浏览器订单')
            ->assertSee('去看待支付订单')
            ->assertSee('/site-admin/orders?payment_status=failed&amp;device_type=mobile&amp;sort=latest', false);

        $this->get('/site-admin/orders?device_type=desktop&payment_status=failed&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦桌面浏览器订单，建议优先关注 PC 端下单流程、页面首屏稳定性与高客单转化表现。')
            ->assertSee('继续查看桌面浏览器订单')
            ->assertSee('去看高金额已支付订单')
            ->assertSee('/site-admin/orders?payment_status=failed&amp;device_type=desktop&amp;sort=latest', false);

        $this->get('/site-admin/orders?payment_status=failed&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦支付失败订单，建议优先排查支付渠道、回调结果与用户重试情况。')
            ->assertSee('继续查看支付失败订单')
            ->assertSee('去看高金额已支付订单')
            ->assertSee('/site-admin/orders?payment_status=failed&amp;sort=latest', false);

        $this->get('/site-admin/orders?payment_status=unpaid&sort=latest')
            ->assertOk()
            ->assertSee('当前筛选已聚焦待支付订单，建议优先跟进下单未支付用户，并观察支付转化时效。')
            ->assertSee('继续查看待支付订单')
            ->assertSee('去看高金额已支付订单')
            ->assertSee('/site-admin/orders?payment_status=unpaid&amp;sort=latest', false);

        $this->get('/site-admin/orders?status=completed&sort=latest')
            ->assertOk()
            ->assertSee('当前正在查看已完成订单，建议复盘高客单成交与复购来源，沉淀更稳定的转化路径。')
            ->assertSee('继续查看已完成订单')
            ->assertSee('去看高金额已支付订单')
            ->assertSee('/site-admin/orders?status=completed&amp;sort=latest', false);
    }

    public function test_site_order_status_cards_can_drive_filtering(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/orders?status=completed')
            ->assertOk()
            ->assertSee('已完成')
            ->assertSee('ORD202603080004')
            ->assertDontSee('ORD202603080001');

        $this->get('/site-admin/orders?status=refunded')
            ->assertOk()
            ->assertSee('暂无订单');
    }

    public function test_site_business_pages_display_empty_state_when_filters_miss(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products?keyword=no-match-keyword')
            ->assertOk()
            ->assertSee('暂无商品');

        $this->get('/site-admin/orders?keyword=no-match-keyword')
            ->assertOk()
            ->assertSee('暂无订单');
    }

    public function test_site_export_endpoints_return_csv_response(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin/products/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $this->get('/site-admin/orders/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');
    }

    public function test_site_invalid_product_export_is_blocked_with_redirect_and_error(): void
    {
        $this->loginAsSiteAdmin();

        $response = $this->get('/site-admin/products/export?min_price=300&max_price=100');
        $response->assertStatus(302);
        $this->assertStringContainsString('/site-admin/products?', $response->headers->get('Location'));
        $this->assertStringContainsString('min_price=300', $response->headers->get('Location'));
        $this->assertStringContainsString('max_price=100', $response->headers->get('Location'));
        $this->assertStringContainsString('sort=latest', $response->headers->get('Location'));

        $this->followRedirects($this->get('/site-admin/products/export?min_price=300&max_price=100'))
            ->assertSee('最低价格不能大于最高价格。')
            ->assertSee('暂无商品');
    }

    public function test_site_invalid_order_export_is_blocked_with_redirect_and_error(): void
    {
        $this->loginAsSiteAdmin();

        $response = $this->get('/site-admin/orders/export?min_pay_amount=300&max_pay_amount=100');
        $response->assertStatus(302);
        $this->assertStringContainsString('/site-admin/orders?', $response->headers->get('Location'));
        $this->assertStringContainsString('min_pay_amount=300', $response->headers->get('Location'));
        $this->assertStringContainsString('max_pay_amount=100', $response->headers->get('Location'));
        $this->assertStringContainsString('sort=latest', $response->headers->get('Location'));

        $this->followRedirects($this->get('/site-admin/orders/export?min_pay_amount=300&max_pay_amount=100'))
            ->assertSee('最低实付金额不能大于最高实付金额。')
            ->assertSee('暂无订单');
    }

    public function test_site_export_contains_scope_summary_and_filtered_content(): void
    {
        $this->loginAsSiteAdmin();

        $productExport = $this->get('/site-admin/products/export?keyword=%E6%BC%94%E7%A4%BA%E5%95%86%E5%93%81&status=published&min_price=150&max_price=220');
        $productExport->assertOk();
        $productContent = $productExport->streamedContent();
        $this->assertStringContainsString('导出信息,站点商品导出', $productContent);
        $this->assertStringContainsString('站点ID,1', $productContent);
        $this->assertStringContainsString('状态,已上架', $productContent);
        $this->assertStringContainsString('分类,全部', $productContent);
        $this->assertStringContainsString('最低价格,¥150.00', $productContent);
        $this->assertStringContainsString('最高价格,¥220.00', $productContent);
        $this->assertStringContainsString('排序,最新创建', $productContent);
        $this->assertStringContainsString('导出商品数,1', $productContent);
        $this->assertStringContainsString('导出总库存,100', $productContent);
        $this->assertStringContainsString('演示商品', $productContent);
        $this->assertStringNotContainsString('精选礼包', $productContent);

        $orderExport = $this->get('/site-admin/orders/export?status=shipped&payment_status=paid&platform=wechat_mini&device_type=mini-program&payment_channel=wechat_pay&min_pay_amount=180&max_pay_amount=190');
        $orderExport->assertOk();
        $orderContent = $orderExport->streamedContent();
        $this->assertStringContainsString('导出信息,站点订单导出', $orderContent);
        $this->assertStringContainsString('站点ID,1', $orderContent);
        $this->assertStringContainsString('订单状态,已发货', $orderContent);
        $this->assertStringContainsString('支付状态,已支付', $orderContent);
        $this->assertStringContainsString('平台,微信小程序', $orderContent);
        $this->assertStringContainsString('设备类型,小程序环境', $orderContent);
        $this->assertStringContainsString('支付渠道,微信支付', $orderContent);
        $this->assertStringContainsString('最低实付金额,¥180.00', $orderContent);
        $this->assertStringContainsString('最高实付金额,¥190.00', $orderContent);
        $this->assertStringContainsString('排序,创建时间倒序', $orderContent);
        $this->assertStringContainsString('导出订单数,1', $orderContent);
        $this->assertStringContainsString('导出已支付订单数,1', $orderContent);
        $this->assertStringContainsString('ORD202603080003', $orderContent);
        $this->assertStringNotContainsString('ORD202603080001', $orderContent);
    }

    public function test_site_product_summary_stats_match_export_summary_for_same_filters(): void
    {
        $this->loginAsSiteAdmin();

        $page = $this->get('/site-admin/products?keyword=%E6%BC%94%E7%A4%BA%E5%95%86%E5%93%81&status=published&min_price=150&max_price=220');
        $page->assertOk()->assertViewHas('summaryStats', function (array $summaryStats) {
            return ($summaryStats['total_products'] ?? null) === 1
                && ($summaryStats['total_stock'] ?? null) === 100
                && (float) ($summaryStats['total_stock_value'] ?? 0) === 19900.0
                && (float) ($summaryStats['average_price'] ?? 0) === 199.0;
        });

        $page->assertSee('商品总数')
            ->assertSee('总库存')
            ->assertSee('总货值')
            ->assertSee('平均售价')
            ->assertSee('19,900.00')
            ->assertSee('199.00');

        $export = $this->get('/site-admin/products/export?keyword=%E6%BC%94%E7%A4%BA%E5%95%86%E5%93%81&status=published&min_price=150&max_price=220');
        $export->assertOk();
        $content = $export->streamedContent();

        $this->assertStringContainsString('导出商品数,1', $content);
        $this->assertStringContainsString('导出总库存,100', $content);
        $this->assertStringContainsString('导出总货值,19900.00', $content);
        $this->assertStringContainsString('导出平均售价,199.00', $content);
    }

    public function test_site_order_summary_stats_match_export_summary_for_same_filters(): void
    {
        $this->loginAsSiteAdmin();

        $page = $this->get('/site-admin/orders?status=shipped&payment_status=paid&platform=wechat_mini&device_type=mini-program&payment_channel=wechat_pay&min_pay_amount=180&max_pay_amount=190');
        $page->assertOk()->assertViewHas('summaryStats', function (array $summaryStats) {
            return ($summaryStats['total_orders'] ?? null) === 1
                && (float) ($summaryStats['total_pay_amount'] ?? 0) === 189.0
                && (float) ($summaryStats['average_order_amount'] ?? 0) === 189.0
                && ($summaryStats['paid_orders'] ?? null) === 1
                && ($summaryStats['failed_payment_orders'] ?? null) === 0;
        });

        $page->assertSee('订单总数')
            ->assertSee('实付总额')
            ->assertSee('平均客单价')
            ->assertSee('已支付订单数')
            ->assertSee('支付失败订单')
            ->assertSee('小程序环境')
            ->assertSee('微信支付')
            ->assertSee('189.00');

        $export = $this->get('/site-admin/orders/export?status=shipped&payment_status=paid&platform=wechat_mini&device_type=mini-program&payment_channel=wechat_pay&min_pay_amount=180&max_pay_amount=190');
        $export->assertOk();
        $content = $export->streamedContent();

        $this->assertStringContainsString('设备类型,小程序环境', $content);
        $this->assertStringContainsString('支付渠道,微信支付', $content);
        $this->assertStringContainsString('导出订单数,1', $content);
        $this->assertStringContainsString('导出实付总额,189.00', $content);
        $this->assertStringContainsString('导出平均客单价,189.00', $content);
        $this->assertStringContainsString('导出已支付订单数,1', $content);
        $this->assertStringContainsString('导出支付失败订单,0', $content);
    }

    public function test_platform_site_list_displays_site_admin_entry_link(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');

        $this->get('/admin/merchants')
            ->assertOk()
            ->assertSee('进入站点后台')
            ->assertSee('当前阶段请使用该站点管理员账号登录');
    }
}
