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

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

        $plan = Plan::query()->create([
            'code' => 'po_create_renewal_default_remark_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' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_DEFAULT_REMARK_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(),
        ]);

        $url = '/admin/platform-orders/create?site_subscription_id=' . $sub->id . '&order_type=renewal';

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

        $expectedRemark = config('saasshop.platform_orders.renewal_order_remark_prefix', '来自订阅：') . $sub->subscription_no;

        $res->assertSee('>' . $expectedRemark . '<', false);
    }

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

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

        $plan = Plan::query()->create([
            'code' => 'po_create_renewal_default_remark_override_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' => 'activated',
            'source' => 'manual',
            'subscription_no' => 'SUB_DEFAULT_REMARK_0002',
            '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(),
        ]);

        $customRemark = '自定义备注：线下补单';
        $url = '/admin/platform-orders/create?site_subscription_id=' . $sub->id
            . '&order_type=renewal'
            . '&remark=' . urlencode($customRemark);

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

        $res->assertSee('>' . $customRemark . '<', false);
    }
}
