<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\Helpers\Utility\RouteList;
use Symfony\Component\HttpFoundation\IpUtils;

use Log;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        function is_valid_ipv6_subnet($subnet) {
            // Step 1: Check if the subnet string is in the valid IPv6 CIDR notation format
            if (!preg_match('/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}\/\d{1,3}$/', $subnet)) {
                return false;
            }
            
            // Step 2: Split the subnet into its address and prefix length components
            list($address, $prefix_length) = explode('/', $subnet);
            
            // Step 3: Convert the IPv6 address to binary
            $binary_address = inet_pton($address);
            if ($binary_address === false) {
                return false;
            }
            
            // Step 4: Convert the prefix length to a binary subnet mask
            $binary_mask = str_repeat('1', $prefix_length) . str_repeat('0', 128 - $prefix_length);
            $binary_mask = pack('H*', str_pad(dechex(bindec($binary_mask)), 32, '0', STR_PAD_LEFT));
            
            // Step 5: Perform a bitwise AND operation between the binary address and mask
            $binary_result = $binary_address & $binary_mask;
            
            // Step 6: Convert the result back to a string representation of an IPv6 address
            $result_address = inet_ntop($binary_result);
            if ($result_address === false) {
                return false;
            }
            
            // Step 7: Check if the result matches the original address
            return ($result_address === $address);
        }

        $this->assertTrue(is_valid_ipv6_subnet('2001:0db8:85a3:0000:0000:8a2e:0370:7335/64'));

    }

}
