<?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 AdminSiteSubscriptionBatchActivateFromShowTest 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_platform_admin_can_batch_activate_syncable_orders_from_subscription_show_page(): void
    {
        $this->loginAsPlatformAdmin();

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

        $sub = SiteSubscription::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'status' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_BATCH_SHOW_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addMonth(),
            'activated_at' => now()->subDay(),
        ]);

        // 订单A：可同步（已支付+已生效+未同步）
        $o1 = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $sub->id,
            'created_by_admin_id' => 1,
            'order_no' => 'PO_SUB_BATCH_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()->subMinutes(5),
            'paid_at' => now()->subMinutes(4),
            'activated_at' => now()->subMinutes(3),
        ]);

        // 订单B：同订阅但不可同步（未支付）
        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $sub->id,
            'created_by_admin_id' => 1,
            'order_no' => 'PO_SUB_BATCH_0002',
            'order_type' => 'renewal',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 10,
            'paid_amount' => 0,
            'placed_at' => now()->subMinutes(2),
        ]);

        $this->post('/admin/platform-orders/batch-activate-subscriptions', [
            'scope' => 'filtered',
            'site_subscription_id' => (string) $sub->id,
            'syncable_only' => '1',
            'limit' => 50,
        ])->assertRedirect();

        $o1->refresh();
        $this->assertNotNull(data_get($o1->meta, 'subscription_activation.subscription_id'));
    }

    public function test_guest_cannot_batch_activate_from_subscription_show_page(): void
    {
        $this->seed();

        $this->post('/admin/platform-orders/batch-activate-subscriptions', [
            'scope' => 'filtered',
            'syncable_only' => '1',
            'limit' => 10,
        ])->assertRedirect('/admin/login');
    }
}
