const configEffect = {
  async generateRealisticConfig(command, devices = []) {
    const timestamp = new Date().toLocaleString();
    let config = `! 配置生成于 ${timestamp}\n`;
    const configTemplates = {
      vlan: {
        pattern: /(vlan|虚拟局域网|虚拟网)\s*(\d+)/i,
        template: (vlanId) =>
          `vlan ${vlanId}\n` +
          `  name VLAN_${vlanId}\n` +
          `  exit\n` +
          `interface Vlan${vlanId}\n` +
          `  description ${vlanId === '10' ? '管理VLAN' : '用户VLAN'}\n` +
          `  ip address 192.168.${vlanId}.1 255.255.255.0\n` +
          `  exit\n`,
      },
      ssh: {
        pattern: /(ssh|安全外壳|远程登录)/i,
        template: () => {
          const password = Math.random().toString(36).slice(2, 10);
          return (
            `ip ssh server\n` +
            `ip ssh version 2\n` +
            `username admin privilege 15 secret 0 ${password}\n` +
            `line vty 0 4\n` +
            `  transport input ssh\n` +
            `  login local\n` +
            `  exit\n`
          );
        },
      },
      port: {
        pattern: /(端口|接口|port|interface)\s*(\d+)/i,
        template: (port) => {
          const isTrunk = /(trunk|干道)/i.test(command);
          const isAccess = /(access|接入)/i.test(command) || !isTrunk;
          const desc = /(上联|uplink)/i.test(command) ? 'Uplink_Port' : 'Access_Port';
          const vlanId = command.match(/vlan\s*(\d+)/i)?.[1] || '10';

          return (
            `interface GigabitEthernet0/${port}\n` +
            `  description ${desc}\n` +
            `  switchport mode ${isTrunk ? 'trunk' : 'access'}\n` +
            `  ${isTrunk ? 'switchport trunk allowed vlan all' : `switchport access vlan ${vlanId}`}\n` +
            `  no shutdown\n` +
            `  exit\n`
          );
        },
      },
      acl: {
        pattern: /(acl|访问控制|防火墙)/i,
        template: () => {
          let targetIP = '192.168.10.10';
          if (devices.length > 0) {
            const randomDevice = devices[Math.floor(Math.random() * devices.length)];
            targetIP = randomDevice.ip;
          }
          return (
            `ip access-list extended PROTECT_SERVERS\n` +
            `  permit tcp any host ${targetIP} eq 22\n` +
            `  permit tcp any host ${targetIP} eq 80\n` +
            `  permit tcp any host ${targetIP} eq 443\n` +
            `  deny ip any any\n` +
            `  exit\n` +
            `interface Vlan10\n` +
            `  ip access-group PROTECT_SERVERS in\n` +
            `  exit\n`
          );
        },
      },
      dhcp: {
        pattern: /(dhcp|动态主机配置)/i,
        template: () => {
          const vlanId = command.match(/vlan\s*(\d+)/i)?.[1] || '10';
          return (
            `ip dhcp pool VLAN_${vlanId}\n` +
            `  network 192.168.${vlanId}.0 255.255.255.0\n` +
            `  default-router 192.168.${vlanId}.1\n` +
            `  dns-server 8.8.8.8 8.8.4.4\n` +
            `  exit\n` +
            `ip dhcp excluded-address 192.168.${vlanId}.1 192.168.${vlanId}.10\n`
          );
        },
      },
      nat: {
        pattern: /(nat|网络地址转换)/i,
        template: () => {
          const publicIp = `203.0.113.${Math.floor(Math.random() * 10) + 1}`;
          return (
            `ip access-list standard NAT_ACL\n` +
            `  permit 192.168.0.0 0.0.255.255\n` +
            `  exit\n` +
            `ip nat inside source list NAT_ACL interface GigabitEthernet0/1 overload\n` +
            `interface GigabitEthernet0/1\n` +
            `  ip address ${publicIp} 255.255.255.248\n` +
            `  ip nat outside\n` +
            `  exit\n` +
            `interface Vlan10\n` +
            `  ip nat inside\n` +
            `  exit\n`
          );
        },
      },
      stp: {
        pattern: /(stp|生成树|spanning-tree)/i,
        template: () => {
          return (
            `spanning-tree mode rapid-pvst\n` +
            `spanning-tree vlan 1-4094 priority 4096\n` +
            `spanning-tree portfast default\n` +
            `spanning-tree portfast bpduguard default\n`
          );
        },
      },
      portSecurity: {
        pattern: /(端口安全|port-security)/i,
        template: () => {
          const port = command.match(/端口\s*(\d+)/i)?.[1] || '1';
          return (
            `interface GigabitEthernet0/${port}\n` +
            `  switchport port-security\n` +
            `  switchport port-security maximum 5\n` +
            `  switchport port-security violation restrict\n` +
            `  switchport port-security mac-address sticky\n` +
            `  exit\n`
          );
        },
      },
      qos: {
        pattern: /(qos|服务质量|流量控制)/i,
        template: () => {
          return (
            `class-map match-all VOICE\n` +
            `  match ip dscp ef\n` +
            `  exit\n` +
            `policy-map QOS_POLICY\n` +
            `  class VOICE\n` +
            `    priority percent 20\n` +
            `  class class-default\n` +
            `    bandwidth percent 80\n` +
            `  exit\n` +
            `interface GigabitEthernet0/1\n` +
            `  service-policy output QOS_POLICY\n` +
            `  exit\n`
          );
        },
      },
      vpn: {
        pattern: /(vpn|虚拟专用网)/i,
        template: () => {
          const vpnId = Math.floor(Math.random() * 1000);
          return (
            `crypto isakmp policy ${vpnId}\n` +
            `  encryption aes 256\n` +
            `  hash sha256\n` +
            `  authentication pre-share\n` +
            `  group 14\n` +
            `  exit\n` +
            `crypto ipsec transform-set VPN_TRANSFORM esp-aes 256 esp-sha256-hmac\n` +
            `  mode tunnel\n` +
            `  exit\n` +
            `crypto map VPN_MAP 10 ipsec-isakmp\n` +
            `  set peer 203.0.113.5\n` +
            `  set transform-set VPN_TRANSFORM\n` +
            `  match address VPN_ACL\n` +
            `  exit\n`
          );
        },
      },
    };
    let matched = false;
    if (/(完整配置|全部配置|all config)/i.test(command)) {
      matched = true;
      config += '! 生成完整校园网络配置\n';
      Object.values(configTemplates).forEach((template) => {
        const result = template.template();
        if (result) {
          config += result;
        }
      });
    } else {
      for (const [key, { pattern, template }] of Object.entries(configTemplates)) {
        const match = command.match(pattern);
        if (match) {
          matched = true;
          config += template(match[2] || match[1] || '');
        }
      }
    }

    if (!matched) {
      config += 'hostname SCHOOL_SWITCH\n';
      config += 'ip domain-name school.local\n';
      config += 'snmp-server community SCHOOL_RO RO\n';
      config += 'ntp server 192.168.1.1\n';
      config += 'logging trap informational\n';
      config += 'logging 192.168.1.10\n';
      config += 'service password-encryption\n';
      config += 'enable secret 0 ' + Math.random().toString(36).slice(2, 12) + '\n';
      config += 'no ip http server\n';
      config += 'no ip http secure-server\n';
    }

    return { config };
  },
};

export default configEffect;
