<?php

namespace Tests\Feature;

use App\Models\Admin;
use App\Models\Merchant;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class AdminDashboardMerchantRevenueRank7dMiniChartShouldHaveLinkSourceInTableTest extends TestCase
{
    use RefreshDatabase;

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

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

    public function test_rank_mini_chart_should_have_link_source_in_table(): void
    {
        Cache::flush();

        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $platformAdminId = (int) Admin::query()->where('email', 'platform.admin@demo.local')->value('id');

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => null,
            'site_subscription_id' => null,
            'created_by_admin_id' => $platformAdminId ?: null,
            'order_no' => 'PO_DASH_RANK_CHART_LINK_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'meta' => [],
        ]);

        Cache::flush();

        $res = $this->get('/admin');
        $res->assertOk();

        // 迷你排行（JS 渐进增强）复用表格中站点链接口径，因此这里做“表格链接存在”护栏。
        $res->assertSee('data-role="merchant-revenue-rank-7d"', false);
        $res->assertSee('data-role="merchant-revenue-rank-7d-chart"', false);

        // 表格中站点名称应为可点击链接，且带 merchant_id 与近7天范围。
        $res->assertSee($merchant->name, false);
        $res->assertSee('merchant_id=' . (int) $merchant->id, false);
        $res->assertSee('created_from=', false);
        $res->assertSee('created_to=', false);
    }
}
