<?php
// mikrotik_api.php
require_once 'vendor/autoload.php'; // If using Composer
// Or use direct socket connection if no Composer

use RouterOS\Client;
use RouterOS\Query;

function configureMikroTikInternetAccess($ip_address, $mac_address, $bandwidth_mbps) {
    global $mikrotik_config;
    
    try {
        // Connect to MikroTik
        $client = new Client([
            'host' => $mikrotik_config['host'],
            'user' => $mikrotik_config['user'],
            'pass' => $mikrotik_config['pass'],
            'port' => $mikrotik_config['port']
        ]);
        
        // 1. Add static ARP entry (IP to MAC binding)
        $arp_query = (new Query('/ip/arp/add'))
            ->equal('address', $ip_address)
            ->equal('mac-address', $mac_address)
            ->equal('interface', 'bridge-lan') // Change to your LAN interface
            ->equal('disabled', 'no');
        
        $client->query($arp_query)->read();
        
        // 2. Add IP address to address list
        $address_list_query = (new Query('/ip/firewall/address-list/add'))
            ->equal('list', $mikrotik_config['address_list'])
            ->equal('address', $ip_address)
            ->equal('timeout', 'none'); // Will be removed when subscription expires
        
        $client->query($address_list_query)->read();
        
        // 3. Set bandwidth limit for this IP (simple queue)
        $queue_query = (new Query('/queue/simple/add'))
            ->equal('name', "queue-$ip_address")
            ->equal('target', $ip_address)
            ->equal('max-limit', "{$bandwidth_mbps}M/{$bandwidth_mbps}M") // Upload/Download
            ->equal('disabled', 'no');
        
        $client->query($queue_query)->read();
        
        // 4. Add DHCP lease with static IP
        $dhcp_query = (new Query('/ip/dhcp-server/lease/add'))
            ->equal('address', $ip_address)
            ->equal('mac-address', $mac_address)
            ->equal('server', 'dhcp-server') // Change to your DHCP server name
            ->equal('disabled', 'no');
        
        $client->query($dhcp_query)->read();
        
        return [
            'success' => true,
            'message' => 'MikroTik configured successfully'
        ];
        
    } catch (Exception $e) {
        return [
            'success' => false,
            'message' => $e->getMessage()
        ];
    }
}

function removeMikroTikInternetAccess($ip_address, $mac_address) {
    global $mikrotik_config;
    
    try {
        $client = new Client([
            'host' => $mikrotik_config['host'],
            'user' => $mikrotik_config['user'],
            'pass' => $mikrotik_config['pass'],
            'port' => $mikrotik_config['port']
        ]);
        
        // Remove from address list
        $query = (new Query('/ip/firewall/address-list/remove'))
            ->where('list', $mikrotik_config['address_list'])
            ->where('address', $ip_address);
        $client->query($query)->read();
        
        // Remove simple queue
        $query = (new Query('/queue/simple/remove'))
            ->where('name', "queue-$ip_address");
        $client->query($query)->read();
        
        // Remove static ARP entry
        $query = (new Query('/ip/arp/remove'))
            ->where('address', $ip_address)
            ->where('mac-address', $mac_address);
        $client->query($query)->read();
        
        return true;
        
    } catch (Exception $e) {
        error_log("Failed to remove MikroTik config: " . $e->getMessage());
        return false;
    }
}

// Alternative using SSH (if API not available)
function configureMikroTikSSH($ip_address, $mac_address, $bandwidth_mbps) {
    global $mikrotik_config;
    
    $commands = [
        "/ip arp add address={$ip_address} mac-address={$mac_address} interface=bridge-lan",
        "/ip firewall address-list add list={$mikrotik_config['address_list']} address={$ip_address}",
        "/queue simple add name=queue-{$ip_address} target={$ip_address} max-limit={$bandwidth_mbps}M/{$bandwidth_mbps}M",
        "/ip dhcp-server lease add address={$ip_address} mac-address={$mac_address} server=dhcp-server"
    ];
    
    $command_string = implode('; ', $commands);
    $ssh_command = "ssh {$mikrotik_config['user']}@{$mikrotik_config['host']} \"$command_string\"";
    
    $output = shell_exec($ssh_command . " 2>&1");
    
    return [
        'success' => true,
        'output' => $output
    ];
}
?>