<?php

namespace Tests\Feature;

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

class AdminSiteSubscriptionShowBatchRunIdShouldLinkToPlatformBatchShowPageTest 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_site_subscription_show_page_batch_run_id_should_link_to_platform_batch_show_page(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'sub_show_batch_run_id_plan_01',
            'name' => '订阅详情批次run_id链接测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $subscription = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'subscription_no' => 'SUB_SHOW_BATCH_RUN_ID_0001',
            'status' => 'active',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDays(1),
            'ends_at' => now()->addDays(30),
            'activated_at' => now()->subDays(1),
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $subscription->id,
            'order_no' => 'PO_SUB_SHOW_BATCH_RUN_ID_0001',
            'order_type' => 'renewal',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 10,
            'placed_at' => now(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'batch_activation' => [
                    'run_id' => 'RUN_BAS_SUB_0001',
                ],
                'batch_mark_paid_and_activate' => [
                    'run_id' => 'RUN_BMPA_SUB_0001',
                ],
            ],
        ]);

        $res = $this->get('/admin/site-subscriptions/' . $subscription->id);
        $res->assertOk();

        // BAS/BMPA run_id 在订阅详情页应可点击直达批次复盘页（携带 back 回到订阅详情）。
        $res->assertSee('RUN_BAS_SUB_0001', false);
        $res->assertSee('/admin/platform-batches/show?type=bas&run_id=RUN_BAS_SUB_0001', false);
        $res->assertSee('back=%2Fadmin%2Fsite-subscriptions%2F' . $subscription->id, false);

        $res->assertSee('RUN_BMPA_SUB_0001', false);
        $res->assertSee('/admin/platform-batches/show?type=bmpa&run_id=RUN_BMPA_SUB_0001', false);
    }
}
