<?php

namespace Tests\Unit\Helpers;

use PHPUnit\Framework\TestCase;
use App\Helpers\DataNormalizationHelper;

class DataNormalizationHelperTest extends TestCase
{
    /**
     * Test normalizing amount with proper rounding.
     *
     * @test
     * @dataProvider amountNormalizationProvider
     */
    public function testNormalizeAmount($input, $expected)
    {
        $result = DataNormalizationHelper::normalizeAmount($input);
        $this->assertEquals($expected, $result);
    }

    public function amountNormalizationProvider()
    {
        return [
            [123.456789, 123.46],
            [1000, 1000.00],
            [0.1, 0.10],
            [999999.999, 1000000.00],
            [-50, -50.00],
            ['123.45', 123.45],
            ['invalid', 0.00],
        ];
    }

    /**
     * Test email normalization and validation.
     *
     * @test
     * @dataProvider emailNormalizationProvider
     */
    public function testNormalizeEmail($input, $expected)
    {
        $result = DataNormalizationHelper::normalizeEmail($input);
        $this->assertEquals($expected, $result);
    }

    public function emailNormalizationProvider()
    {
        return [
            ['John@Example.COM', 'john@example.com'],
            ['  user@domain.com  ', 'user@domain.com'],
            ['Test.User@Test.Com', 'test.user@test.com'],
            ['invalid.email', ''],
            ['', ''],
        ];
    }

    /**
     * Test phone normalization.
     *
     * @test
     * @dataProvider phoneNormalizationProvider
     */
    public function testNormalizePhone($input, $expected)
    {
        $result = DataNormalizationHelper::normalizePhone($input);
        $this->assertEquals($expected, $result);
    }

    public function phoneNormalizationProvider()
    {
        return [
            ['0123456789', '+2250123456789'],
            ['+225 123 456 789', '+225123456789'],
            ['123456789', '+225123456789'],
            ['invalid', ''],
            ['', ''],
        ];
    }

    /**
     * Test date formatting.
     *
     * @test
     */
    public function testFormatDate()
    {
        $date = \DateTime::createFromFormat('Y-m-d H:i:s', '2025-12-31 15:30:45');
        $result = DataNormalizationHelper::formatDate($date);
        $this->assertEquals('31/12/2025 15:30:45', $result);
    }

    /**
     * Test name normalization.
     *
     * @test
     * @dataProvider nameNormalizationProvider
     */
    public function testNormalizeName($input, $expected)
    {
        $result = DataNormalizationHelper::normalizeName($input);
        $this->assertEquals($expected, $result);
    }

    public function nameNormalizationProvider()
    {
        return [
            ['john doe', 'John Doe'],
            ['MARIE MARTIN', 'Marie Martin'],
            ['  patrick  ', 'Patrick'],
            ['jean-paul', 'Jean-Paul'],
        ];
    }

    /**
     * Test amount validation.
     *
     * @test
     * @dataProvider amountValidationProvider
     */
    public function testValidateAmount($amount, $min, $max, $expected)
    {
        $result = DataNormalizationHelper::validateMinimumAmount($amount, $min);
        $this->assertEquals($expected, $result);
    }

    public function amountValidationProvider()
    {
        return [
            [5000, 5000, 999999, true],
            [4999, 5000, 999999, false],
            [1000000, 5000, 999999, false],
            [500000, 5000, 999999, true],
        ];
    }

    /**
     * Test URL validation.
     *
     * @test
     * @dataProvider urlValidationProvider
     */
    public function testValidateUrl($url, $expected)
    {
        $result = DataNormalizationHelper::validateUrl($url);
        $this->assertEquals($expected, $result);
    }

    public function urlValidationProvider()
    {
        return [
            ['https://example.com', true],
            ['http://domain.co.uk', true],
            ['not a url', false],
            ['://incomplete', false],
            ['ftp://example.com', true], // FTP is technically a URL
        ];
    }

    /**
     * Test country code validation.
     *
     * @test
     */
    public function testNormalizeCountryCode()
    {
        $valid = DataNormalizationHelper::normalizeCountryCode('BF');
        $this->assertEquals('BF', $valid);

        $invalid = DataNormalizationHelper::normalizeCountryCode('XX');
        $this->assertEquals('', $invalid);

        $lowercase = DataNormalizationHelper::normalizeCountryCode('ci');
        $this->assertEquals('CI', $lowercase);
    }
}
