<?php

namespace Tests\Unit\B2B;

use Mtc\Modules\B2B\Enums\AccountOrderStatus;
use Mtc\Modules\B2B\Models\AccountOrder;
use Mtc\Modules\B2B\Models\TradeCustomer;
use Mtc\Modules\B2B\Models\TradeTier;
use Mtc\Modules\Members\Models\Member;
use Mtc\Shop\Order;
use Tests\TestCase;

class AccountOrderModelTest extends TestCase
{

    private function createTradeCustomer(): TradeCustomer
    {
        $member = new Member();
        $member->email = 'test-' . uniqid() . '@example.com';
        $member->save();

        $tier = TradeTier::create([
            'name' => 'Bronze',
            'discount_percentage' => 5,
            'sort_order' => 1,
        ]);

        return TradeCustomer::create([
            'member_id' => $member->id,
            'tier_id' => $tier->id,
            'company_name' => 'Test Co',
            'phone' => '07700900000',
            'status' => 'approved',
        ]);
    }

    public function test_unpaid_scope()
    {
        $customer = $this->createTradeCustomer();

        AccountOrder::create([
            'order_id' => rand(900000, 999999),
            'trade_customer_id' => $customer->id,
            'po_number' => 'PO-00001',
            'amount_due' => 100.00,
            'status' => 'unpaid',
        ]);

        AccountOrder::create([
            'order_id' => rand(900000, 999999),
            'trade_customer_id' => $customer->id,
            'po_number' => 'PO-00002',
            'amount_due' => 50.00,
            'status' => 'paid',
        ]);

        $unpaid = AccountOrder::unpaid()->where('trade_customer_id', $customer->id)->get();
        $this->assertCount(1, $unpaid);
        $this->assertEquals('PO-00001', $unpaid->first()->po_number);
    }

    public function test_paid_scope()
    {
        $customer = $this->createTradeCustomer();

        AccountOrder::create([
            'order_id' => rand(900000, 999999),
            'trade_customer_id' => $customer->id,
            'po_number' => 'PO-00001',
            'amount_due' => 100.00,
            'status' => 'paid',
        ]);

        $paid = AccountOrder::paid()->where('trade_customer_id', $customer->id)->get();
        $this->assertCount(1, $paid);
    }

    public function test_mark_paid_method()
    {
        $customer = $this->createTradeCustomer();

        $order = AccountOrder::create([
            'order_id' => rand(900000, 999999),
            'trade_customer_id' => $customer->id,
            'po_number' => 'PO-00001',
            'amount_due' => 100.00,
            'status' => 'unpaid',
        ]);

        $order->markPaid('card', 'REF-123');

        $order->refresh();
        $this->assertEquals(AccountOrderStatus::Paid, $order->status);
        $this->assertEquals('card', $order->paid_by);
        $this->assertEquals('REF-123', $order->payment_reference);
        $this->assertNotNull($order->paid_at);
    }

    public function test_trade_customer_relationship()
    {
        $customer = $this->createTradeCustomer();

        $order = AccountOrder::create([
            'order_id' => rand(900000, 999999),
            'trade_customer_id' => $customer->id,
            'po_number' => 'PO-00001',
            'amount_due' => 100.00,
            'status' => 'unpaid',
        ]);

        $this->assertInstanceOf(TradeCustomer::class, $order->tradeCustomer);
        $this->assertEquals($customer->id, $order->tradeCustomer->id);
    }
}
