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

        $merchant = Merchant::query()->firstOrFail();

        $plan = Plan::query()->create([
            'code' => 'po_renewal_missing_sub_filter_plan',
            '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' => 'active',
            'source' => 'manual',
            'subscription_no' => 'SS_PO_FILTER_SUB_0001',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'amount' => 10,
            'starts_at' => now()->subDays(10),
            'ends_at' => now()->addDays(20),
            'snapshot' => [],
            'meta' => [],
        ]);

        $shouldShow = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RENEWAL_MISSING_SUB_SHOW_0001',
            'order_type' => 'renewal',
            'site_subscription_id' => null,
            '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(10),
            'meta' => [],
        ]);

        $renewalWithSubShouldNotShow = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $sub->id,
            'order_no' => 'PO_RENEWAL_WITH_SUB_HIDE_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(9),
            'meta' => [],
        ]);

        $newPurchaseWithoutSubShouldNotShow = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_NEW_NO_SUB_HIDE_0003',
            'order_type' => 'new_purchase',
            'site_subscription_id' => null,
            '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(8),
            'meta' => [],
        ]);

        $this->get('/admin/platform-orders?renewal_missing_subscription=1')
            ->assertOk()
            ->assertSee($shouldShow->order_no)
            ->assertDontSee($renewalWithSubShouldNotShow->order_no)
            ->assertDontSee($newPurchaseWithoutSubShouldNotShow->order_no);
    }
}
