<?php

namespace Tests\Feature;

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

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

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'receipt_test',
            'name' => '回执测试（月付）',
            'billing_cycle' => 'monthly',
            'price' => 30,
            'list_price' => 30,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECEIPT_0001',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 30,
            'paid_amount' => 0,
            'placed_at' => now(),
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/add-payment-receipt', [
            'type' => 'bank_transfer',
            'channel' => 'icbc',
            'amount' => 30,
            'paid_at' => now()->format('Y-m-d H:i:s'),
            'note' => '测试回执',
        ])->assertRedirect();

        $order->refresh();

        $receipts = (array) data_get($order->meta, 'payment_receipts', []);
        $this->assertCount(1, $receipts);
        $this->assertSame('bank_transfer', data_get($receipts[0], 'type'));
        $this->assertSame('icbc', data_get($receipts[0], 'channel'));
        $this->assertSame(30.0, (float) data_get($receipts[0], 'amount'));
        $this->assertSame('测试回执', data_get($receipts[0], 'note'));
        $this->assertNotEmpty((string) data_get($receipts[0], 'created_at'));
        $this->assertNotEmpty((string) data_get($receipts[0], 'admin_id'));
    }

    public function test_guest_cannot_add_payment_receipt(): void
    {
        $this->seed();

        $merchant = Merchant::query()->firstOrFail();
        $plan = Plan::query()->create([
            'code' => 'receipt_test_guest',
            'name' => '回执测试（游客）',
            'billing_cycle' => 'monthly',
            'price' => 30,
            'list_price' => 30,
            'status' => 'active',
            'sort' => 10,
            'published_at' => now(),
        ]);

        $order = PlatformOrder::query()->create([
            'merchant_id' => $merchant->id,
            'plan_id' => $plan->id,
            'order_no' => 'PO_RECEIPT_0002',
            'order_type' => 'new_purchase',
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'plan_name' => $plan->name,
            'billing_cycle' => $plan->billing_cycle,
            'period_months' => 1,
            'quantity' => 1,
            'payable_amount' => 30,
            'paid_amount' => 0,
            'placed_at' => now(),
        ]);

        $this->post('/admin/platform-orders/' . $order->id . '/add-payment-receipt', [
            'type' => 'bank_transfer',
            'amount' => 30,
        ])->assertRedirect('/admin/login');
    }
}
