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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_mark_activated_block_fail_only_plan',
            'name' => '批量仅标记生效阻断（fail_only）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $o = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMA_BLOCK_FAIL_ONLY_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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(5),
            'meta' => [],
        ]);

        $this->post('/admin/platform-orders/batch-mark-activated', [
            'scope' => 'filtered',
            'payment_status' => 'paid',
            'status' => 'pending',
            'sync_status' => 'unsynced',
            'fail_only' => '1',
            'limit' => 50,
        ])
            ->assertRedirect()
            ->assertSessionHas('warning');

        $o->refresh();
        $this->assertSame('pending', $o->status);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_mark_activated_block_synced_only_plan',
            'name' => '批量仅标记生效阻断（synced_only）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $o = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMA_BLOCK_SYNCED_ONLY_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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(5),
            'meta' => [],
        ]);

        $this->post('/admin/platform-orders/batch-mark-activated', [
            'scope' => 'filtered',
            'payment_status' => 'paid',
            'status' => 'pending',
            'sync_status' => 'unsynced',
            'synced_only' => '1',
            'limit' => 50,
        ])
            ->assertRedirect()
            ->assertSessionHas('warning');

        $o->refresh();
        $this->assertSame('pending', $o->status);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'batch_mark_activated_block_syncable_only_plan',
            'name' => '批量仅标记生效阻断（syncable_only）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 10,
            'list_price' => 10,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $o = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_BMA_BLOCK_SYNCABLE_ONLY_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            '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(5),
            'meta' => [],
        ]);

        $this->post('/admin/platform-orders/batch-mark-activated', [
            'scope' => 'filtered',
            'payment_status' => 'paid',
            'status' => 'pending',
            'sync_status' => 'unsynced',
            'syncable_only' => '1',
            'limit' => 50,
        ])
            ->assertRedirect()
            ->assertSessionHas('warning');

        $o->refresh();
        $this->assertSame('pending', $o->status);
    }
}
