<?php

namespace Tests\Feature;

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

class AdminDashboardMerchantRevenueRank7dShouldLinkToPlatformOrdersListByMerchantTest 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_dashboard_merchant_revenue_rank_7d_should_link_to_platform_orders_list_by_merchant(): void
    {
        $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_LINK_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'paid',
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'meta' => [],
        ]);

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

        $res->assertSee($merchant->name, false);

        // 断言 Top5 站点链接能带 merchant_id 与 created_from/to（近7天）
        $res->assertSee('merchant_id=' . (int) $merchant->id, false);
        $res->assertSee('created_from=', false);
        $res->assertSee('created_to=', false);
        $res->assertSee('/admin/platform-orders?', false);
    }
}
