<?php

namespace Tests\Unit\B2B;

use Mtc\Modules\B2B\Services\PoNumberService;
use Tests\TestCase;

class PoNumberServiceTest extends TestCase
{

    private PoNumberService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = new PoNumberService();
    }

    protected function tearDown(): void
    {
        config(['b2b.po_prefix' => null]);
        parent::tearDown();
    }

    public function test_first_po_number_format()
    {
        $po = $this->service->generate();

        $this->assertMatchesRegularExpression('/^PO-\d{5}$/', $po);
    }

    public function test_sequential_generation()
    {
        $po1 = $this->service->generate();
        $po2 = $this->service->generate();

        // Extract numbers
        $num1 = (int) substr($po1, 3);
        $num2 = (int) substr($po2, 3);

        $this->assertEquals($num1 + 1, $num2);
    }

    public function test_custom_prefix_from_config()
    {
        config(['b2b.po_prefix' => 'INV']);

        $po = $this->service->generate();

        $this->assertStringStartsWith('INV-', $po);
    }

    public function test_po_number_is_padded()
    {
        $po = $this->service->generate();

        // Should be 5 digits after prefix
        $number = substr($po, strpos($po, '-') + 1);
        $this->assertEquals(5, strlen($number));
    }
}
