<?php

namespace Tests\Feature;

use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Plugins\Neatstudio\Membership\Models\MembershipPlan;
use Plugins\Neatstudio\Membership\Models\Subscription;
use Plugins\Neatstudio\Payment\Models\Payment;
use Tests\TestCase;

class MembershipFlowTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

    private MembershipPlan $plan;

    protected function setUp(): void
    {
        parent::setUp();

        $this->seed();
        Setting::set('pay_sandbox', '1');

        $this->user = User::create([
            'name' => '测试会员',
            'email' => 'member@test.com',
            'password' => bcrypt('password'),
        ]);

        $this->plan = MembershipPlan::create([
            'name' => '月付会员',
            'slug' => 'monthly',
            'price' => 9900,
            'duration_days' => 30,
            'permissions' => ['read_members_only'],
            'active' => true,
        ]);
    }

    public function test_membership_page_lists_plans(): void
    {
        $this->get('/membership')->assertOk()->assertSee('月付会员');
    }

    public function test_subscribe_creates_payment_and_redirects(): void
    {
        $this->actingAs($this->user)
            ->post('/membership/'.$this->plan->id.'/subscribe')
            ->assertRedirect();

        $payment = Payment::query()->where('user_id', $this->user->id)->first();
        $this->assertNotNull($payment);
        $this->assertSame('pending', $payment->status);
        $this->assertSame('MembershipPlan#'.$this->plan->id, $payment->description);
    }

    public function test_sandbox_payment_activates_subscription(): void
    {
        $this->actingAs($this->user)->post('/membership/'.$this->plan->id.'/subscribe');
        $payment = Payment::query()->where('user_id', $this->user->id)->first();

        $this->actingAs($this->user)->get('/pay/checkout/'.$payment->id)->assertRedirect();
        $this->actingAs($this->user)->post('/pay/sandbox/'.$payment->order_no.'/confirm')->assertRedirect();

        $payment->refresh();
        $this->assertSame('paid', $payment->status);

        $subscription = Subscription::query()->where('user_id', $this->user->id)->first();
        $this->assertNotNull($subscription);
        $this->assertTrue($subscription->isActive());
    }

    public function test_paid_content_hidden_for_guest_and_visible_for_member(): void
    {
        $post = Post::create([
            'title' => '付费文章',
            'slug' => 'paid-post',
            'content' => "公开内容\n\n[paid]\n付费隐藏内容\n[/paid]",
            'content_format' => 'markdown',
            'status' => 'published',
            'published_at' => now(),
        ]);

        // 游客：付费部分被替换
        $this->get('/posts/paid-post.shtml')
            ->assertOk()
            ->assertDontSee('付费隐藏内容')
            ->assertSee('付费内容已隐藏');

        // 会员：可见
        $this->actingAs($this->user)->post('/membership/'.$this->plan->id.'/subscribe');
        $payment = Payment::query()->where('user_id', $this->user->id)->first();
        $this->actingAs($this->user)->post('/pay/sandbox/'.$payment->order_no.'/confirm');

        $this->actingAs($this->user)->get('/posts/paid-post.shtml')
            ->assertOk()
            ->assertSee('付费隐藏内容');
    }
}
