<?php

namespace Tests\Feature;

use App\Jobs\BatchActivateSubscriptionsJob;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use App\Support\SubscriptionActivationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'job_batch_activate_last_result_plan',
            'name' => 'Job 批量同步 last_result 测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $ok = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_JOB_LAST_RESULT_OK_0001',
            'order_type' => 'new_purchase',
            '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()->subMinutes(10),
            'paid_at' => now()->subMinutes(9),
            'activated_at' => now()->subMinutes(8),
            'meta' => [],
        ]);

        $bad = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_JOB_LAST_RESULT_BAD_0002',
            'order_type' => 'new_purchase',
            '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()->subMinutes(7),
            'paid_at' => now()->subMinutes(6),
            'activated_at' => now()->subMinutes(5),
            'meta' => [],
        ]);

        // 绑定一个假的 service，让它对 bad 抛异常，以便验证 top_reasons 写入
        $badId = $bad->id;
        $fakeService = new class($badId) extends SubscriptionActivationService {
            public function __construct(private int $badId) {}

            public function activateOrder(int $orderId, ?int $adminId = null): \App\Models\SiteSubscription
            {
                if ($orderId === $this->badId) {
                    throw new \RuntimeException('模拟失败：订阅同步异常');
                }

                return parent::activateOrder($orderId, $adminId);
            }
        };

        $job = new BatchActivateSubscriptionsJob([
            $ok->id,
            $bad->id,
        ], 1, 'filtered', 'syncable_only=1', 50, 2, 2);

        $job->handle($fakeService);

        $ok->refresh();
        $bad->refresh();

        $this->assertNotEmpty((string) data_get($ok->meta, 'batch_activation.run_id'));

        $this->assertSame(1, (int) data_get($ok->meta, 'batch_activation.last_result.success'));
        $this->assertSame(1, (int) data_get($ok->meta, 'batch_activation.last_result.failed'));
        $this->assertSame(2, (int) data_get($ok->meta, 'batch_activation.last_result.processed'));

        $top = (array) (data_get($ok->meta, 'batch_activation.last_result.top_reasons', []) ?? []);
        $this->assertNotEmpty($top);
        $this->assertSame('模拟失败：订阅同步异常', (string) data_get($top, '0.reason'));
        $this->assertSame(1, (int) data_get($top, '0.count'));

        // bad 订单也应写入同一个 last_result（同批次 run_id）
        $this->assertSame(
            (string) data_get($ok->meta, 'batch_activation.last_result.run_id'),
            (string) data_get($bad->meta, 'batch_activation.last_result.run_id')
        );
    }
}
