<?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 Illuminate\Support\Arr;
use Tests\TestCase;

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

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

        $plan = Plan::query()->create([
            'code' => 'plan_link_back_test',
            'name' => '套餐联动 back 测试',
            'billing_cycle' => 'monthly',
            'price' => 9.9,
            'list_price' => 9.9,
            'status' => 'active',
            'sort' => 1,
            'published_at' => now(),
        ]);

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

        PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'site_subscription_id' => $sub->id,
            'order_no' => 'PO_PLAN_LINK_BACK_0001',
            'order_type' => 'new_purchase',
            'status' => 'activated',
            'payment_status' => 'paid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'list_amount' => 9.9,
            'discount_amount' => 0,
            'payable_amount' => 9.9,
            'paid_amount' => 9.9,
            'placed_at' => now()->subHour(),
            'paid_at' => now()->subMinutes(30),
            'activated_at' => now()->subMinutes(20),
        ]);

        $back = '/admin/plans?status=active';

        $res = $this->get($back);
        $res->assertOk();

        $expectedSubscriptionUrl = '/admin/site-subscriptions?' . Arr::query([
            'plan_id' => $plan->id,
            'back' => $back,
        ]);

        $expectedOrderUrl = '/admin/platform-orders?' . Arr::query([
            'plan_id' => $plan->id,
            'back' => $back,
        ]);

        $res->assertSee($expectedSubscriptionUrl, false);
        $res->assertSee($expectedOrderUrl, false);
    }
}
