<?php

namespace Tests\Feature;

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

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

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

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_IDX_BATCH_SYNC_BTN_DISABLE_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(),
            'paid_at' => now(),
            'activated_at' => now(),
            'meta' => [
                'payment_summary' => [
                    'count' => 1,
                    'total_amount' => 10,
                ],
            ],
        ]);

        // 未勾选 syncable_only=1
        $res = $this->get('/admin/platform-orders?payment_status=paid&status=activated');
        $res->assertOk();

        $html = (string) $res->getContent();

        $this->assertStringContainsString('批量同步订阅（当前筛选范围）', $html);
        $this->assertStringContainsString('disabled', $html);
        $this->assertStringContainsString('data-role="batch-activate-subscriptions-blocked-hint"', $html);
        $this->assertStringContainsString('只看可同步', $html);
    }

    public function test_batch_activate_button_should_disable_when_receipt_status_none(): void
    {
        $this->loginAsPlatformAdmin();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'po_index_batch_sync_btn_disable_receipt_none_plan',
            'name' => '批量同步按钮禁用无回执（广义）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_IDX_BATCH_SYNC_BTN_DISABLE_RC_NONE_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(),
            'paid_at' => now(),
            'activated_at' => now(),
            // meta 为空 => 无回执（广义）
            'meta' => [],
        ]);

        // 勾选 syncable_only=1 且 receipt_status=none
        $res = $this->get('/admin/platform-orders?syncable_only=1&receipt_status=none');
        $res->assertOk();

        $html = (string) $res->getContent();

        $this->assertStringContainsString('批量同步订阅（当前筛选范围）', $html);
        $this->assertStringContainsString('disabled', $html);
        $this->assertStringContainsString('无回执（广义）', $html);
        $this->assertStringContainsString('data-role="batch-activate-subscriptions-blocked-hint"', $html);
    }
}
