<?php

namespace Tests\Feature;

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

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'sub_batch_mark_expired_block_scope_plan',
            'name' => '订阅批量标记过期阻断（scope）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 1,
            'list_price' => 1,
            '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_EXPIRED_BLOCK_SCOPE_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->addDays(5),
            'activated_at' => now()->subDay(),
        ]);

        $this->post('/admin/site-subscriptions/batch-mark-expired', [
            'expiry' => 'expiring_7d',
            'confirm' => 'YES',
        ])->assertRedirect()->assertSessionHas('warning');

        $sub->refresh();
        $this->assertSame('activated', (string) $sub->status);
    }

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'sub_batch_mark_expired_block_confirm_plan',
            'name' => '订阅批量标记过期阻断（confirm）测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 1,
            'list_price' => 1,
            '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_EXPIRED_BLOCK_CONFIRM_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 1,
            'starts_at' => now()->subDay(),
            'ends_at' => now()->subDays(1),
            'activated_at' => now()->subDay(),
        ]);

        $this->post('/admin/site-subscriptions/batch-mark-expired', [
            'expiry' => 'expired',
            'confirm' => '',
        ])->assertRedirect()->assertSessionHas('warning');

        $sub->refresh();
        $this->assertSame('activated', (string) $sub->status);
    }
}
