<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Cart\LegacyOrderImport;

use CpCreation\VitiCore\Cart\LegacyOrderImport\LegacyOrderSourceConnectionConfig;
use PHPUnit\Framework\TestCase;

final class LegacyOrderSourceConnectionConfigTest extends TestCase
{
    public function testBuildsDirectDbalParametersWithoutAUrl(): void
    {
        $config = new LegacyOrderSourceConnectionConfig(
            database: 'legacy_shop',
            user: 'legacy_user',
            password: 'secret:@/ with spaces',
            host: 'mysql.example.test',
            port: 3307,
        );

        self::assertSame([
            'charset' => 'utf8mb4',
            'dbname' => 'legacy_shop',
            'driver' => 'pdo_mysql',
            'host' => 'mysql.example.test',
            'password' => 'secret:@/ with spaces',
            'port' => 3307,
            'user' => 'legacy_user',
        ], $config->dbalParameters());
        self::assertSame('legacy_user@mysql.example.test:3307/legacy_shop', $config->mysqlEndpoint());
        self::assertNull($config->sshEndpoint());
    }

    public function testBuildsAnSshCommandAndConnectsDbalOnlyToTheLocalTunnel(): void
    {
        $config = new LegacyOrderSourceConnectionConfig(
            database: 'legacy_shop',
            user: 'legacy_user',
            password: 'mysql-secret',
            host: '127.0.0.1',
            port: 3306,
            sshHost: 'server2.cpcreation.fr',
            sshUser: 'ssh_user',
            sshKey: '/root/.ssh/server2',
            sshPort: 2222,
        );

        $command = $config->sshCommand(49152, '/usr/bin/ssh');

        self::assertSame('/usr/bin/ssh', $command[0]);
        self::assertContains('127.0.0.1:49152:127.0.0.1:3306', $command);
        self::assertContains('ssh_user@server2.cpcreation.fr', $command);
        self::assertContains('/root/.ssh/server2', $command);
        self::assertNotContains('mysql-secret', $command);
        self::assertSame([
            'charset' => 'utf8mb4',
            'dbname' => 'legacy_shop',
            'driver' => 'pdo_mysql',
            'host' => '127.0.0.1',
            'password' => 'mysql-secret',
            'port' => 49152,
            'user' => 'legacy_user',
        ], $config->dbalParameters(49152));
        self::assertSame('ssh_user@server2.cpcreation.fr:2222', $config->sshEndpoint());
    }

    public function testRejectsSshCredentialsWithoutAnSshHost(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Renseignez --ssh-host');

        new LegacyOrderSourceConnectionConfig(
            database: 'legacy_shop',
            user: 'legacy_user',
            password: 'mysql-secret',
            sshUser: 'ssh_user',
        );
    }

    public function testRejectsInvalidPorts(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Le port MySQL source doit etre compris entre 1 et 65535.');

        new LegacyOrderSourceConnectionConfig(
            database: 'legacy_shop',
            user: 'legacy_user',
            password: 'mysql-secret',
            port: 0,
        );
    }
}
