# frozen_string_literal: true

require 'spec_helper'
require 'securerandom'

RSpec.describe LocalTaskExecution, type: :model do
  it '输入 task_info，输出执行单元结构' do
    puts "\n"
    puts "=" * 80
    puts "测试: LocalTaskExecution 从 task_info 解析执行单元"
    puts "=" * 80

    # ==========================================
    # 第一步: 准备测试数据
    # ==========================================
    puts "\n【准备测试数据】"

    # 1. 创建测试设备
    test_device = LocalDevice.new(
      name: "测试路由器",
      ip_address: '192.168.1.100',
      port: 23,
      connection_type: 'telnet',
      username: 'admin',
      password: 'secret123',
      enable_password: 'enable456',
      active: true
    )
    test_device.save
    puts "  创建测试设备: id=#{test_device.id}, name=#{test_device.name}"

    # 2. 创建测试脚本
    test_script = CachedScript.new(
      name: '版本检查脚本',
      description: '检查设备版本信息',
      code: 'show version',
      version: '1.0'
    )
    test_script.id = SecureRandom.uuid
    test_script.save
    puts "  创建测试脚本: id=#{test_script.id}, name=#{test_script.name}"

    # 3. 创建测试任务（包含 task_info)
    task_info_data = {
      devices: [
        {
          id: test_device.id,
          name: test_device.name,
          device_model: 'Huawei AR2200'
        }
      ],
      scripts: [
        {
          id: test_script.id,
          name: test_script.name,
          description: test_script.description
        }
      ]
    }

    test_task = LocalTask.new(
      name: '测试任务',
      description: '测试执行单元解析',
      task_definition: {
        task_json: task_info_data
      },
      device_count: 1
    )
    test_task.id = SecureRandom.uuid
    test_task.save
    puts "  创建测试任务: id=#{test_task.id}"

    # ==========================================
    # 第二步: 显示输入数据
    # ==========================================
    puts "\n【输入数据】"
    puts "  task_info = #{JSON.pretty_generate(task_info_data)}"

    # ==========================================
    # 第三步: 执行核心功能
    # ==========================================
    puts "\n【执行】调用 LocalTaskExecution.create_for_task(task.id)"
    execution = LocalTaskExecution.create_for_task(test_task.id)
    puts "  执行完成"

    # ==========================================
    # 第四步: 显示输出结果
    # ==========================================
    puts "\n【输出结果】"
    puts "  execution_units = #{JSON.pretty_generate(execution.execution_units)}"

    # ==========================================
    # 第五步: 验证结果
    # ==========================================
    puts "\n【验证】"
    
    units = execution.execution_units
    device_id = test_device.id
    device_info = units[device_id] || units[device_id.to_s]
    
    puts "  1. 检查是否包含网元 #{device_id}..."
    expect(device_info).not_to be_nil, "执行单元中应该包含网元 #{device_id}"
    puts "    通过"
    
    puts "  2. 检查网元信息..."
    expect(device_info['name'] || device_info[:name]).to eq(test_device.name)
    puts "    通过"
    
    puts "  3. 检查连接信息中的密码解密..."
    connection_info = device_info['connection_info'] || device_info[:connection_info]
    expect(connection_info['password'] || connection_info[:password]).to eq('secret123')
    expect(connection_info['enable_password'] || connection_info[:enable_password]).to eq('enable456')
    puts "    通过"
    
    puts "  4. 检查脚本列表..."
    scripts = device_info['scripts'] || device_info[:scripts]
    script_id = test_script.id
    script_info = scripts[script_id] || scripts[script_id.to_s]
    expect(script_info).not_to be_nil
    puts "    通过"
    
    puts "  5. 检查脚本状态..."
    expect(script_info['status'] || script_info[:status]).to eq('pending')
    puts "    通过"
    
    puts "  6. 检查日志路径..."
    expect(script_info['log_file_path'] || script_info[:log_file_path]).not_to be_nil
    puts "    通过"

    puts "\n"
    puts "=" * 80
    puts "所有验证通过"
    puts "=" * 80

    # 清理
    LocalTaskExecution.dataset.delete
    LocalTask.dataset.delete
    CachedScript.dataset.delete
    LocalDevice.dataset.delete
  end
end
