<?php

namespace Tests\Unit;

use App\Jobs\AutoConnectMikrotikJob;
use App\Jobs\GenerateInvoiceJob;
use App\Jobs\SyncMikrotikProfilesJob;
use App\Jobs\SyncMikrotikRouterDataJob;
use App\Jobs\TestMikrotikConnectionJob;
use App\Rules\HostOrIpAddress;
use App\Services\MikrotikApiService;
use Illuminate\Support\Facades\Validator;
use ReflectionMethod;
use Tests\TestCase;

class MikrotikCustomPortTest extends TestCase
{
    public function test_custom_api_port_is_not_replaced_by_default_port(): void
    {
        $method = new ReflectionMethod(MikrotikApiService::class, 'normalizeConfig');
        $config = $method->invoke(app(MikrotikApiService::class), [
            'ip_address' => '192.0.2.10',
            'api_port' => 195,
            'username' => 'billing',
            'password' => 'secret',
            'ssl_enabled' => false,
        ]);

        $this->assertSame(195, $config['port']);
        $this->assertSame('192.0.2.10', $config['host']);
    }

    public function test_mikrotik_endpoint_accepts_hostname_and_rejects_url(): void
    {
        $valid = Validator::make(['host' => 'core.ecomradeit.com'], ['host' => [new HostOrIpAddress]]);
        $invalid = Validator::make(['host' => 'https://core.ecomradeit.com:195'], ['host' => [new HostOrIpAddress]]);

        $this->assertTrue($valid->passes());
        $this->assertFalse($invalid->passes());
    }

    public function test_all_statically_dispatched_jobs_expose_dispatch_method(): void
    {
        foreach ([
            SyncMikrotikProfilesJob::class,
            SyncMikrotikRouterDataJob::class,
            AutoConnectMikrotikJob::class,
            TestMikrotikConnectionJob::class,
            GenerateInvoiceJob::class,
        ] as $job) {
            $this->assertTrue(method_exists($job, 'dispatch'), $job.' is missing Dispatchable.');
        }
    }
}
