import { describe, it, expect } from 'vitest';
import { buildSystemPrompt } from '@/lib/token/system-prompt';

describe('buildSystemPrompt', () => {
  it('should contain "No devices connected" when devices array is empty', () => {
    // Arrange
    const ctx = { userName: 'Alice', plan: 'pro', devices: [] };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('No devices connected');
    expect(prompt).toContain('llmhosts up');
  });

  it('should include device name, status, hardware, models, and URL for one device', () => {
    // Arrange
    const ctx = {
      userName: 'Bob',
      plan: 'free',
      devices: [
        {
          name: 'home-server',
          status: 'online',
          models: ['llama3.3:70b', 'qwen3:8b'],
          hardware: 'RTX 4090 24GB',
          tunnelUrl: 'https://home-server.llmhosts.com',
        },
      ],
    };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('home-server');
    expect(prompt).toContain('online');
    expect(prompt).toContain('RTX 4090 24GB');
    expect(prompt).toContain('llama3.3:70b, qwen3:8b');
    expect(prompt).toContain('https://home-server.llmhosts.com');
    expect(prompt).toContain('Fleet Status');
    expect(prompt).not.toContain('No devices connected');
  });

  it('should list all devices when multiple are provided', () => {
    // Arrange
    const ctx = {
      userName: 'Carol',
      plan: 'team',
      devices: [
        {
          name: 'workstation-1',
          status: 'online',
          models: ['qwen3:8b'],
          hardware: 'RTX 3060 12GB',
          tunnelUrl: 'https://ws1.llmhosts.com',
        },
        {
          name: 'server-rack',
          status: 'offline',
          models: ['llama3.3:70b', 'deepseek-r1:32b'],
          hardware: 'A100 80GB',
          tunnelUrl: 'https://rack.llmhosts.com',
        },
        {
          name: 'mini-pc',
          status: 'online',
          models: [],
          hardware: 'Intel iGPU',
          tunnelUrl: 'https://mini.llmhosts.com',
        },
      ],
    };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('workstation-1');
    expect(prompt).toContain('server-rack');
    expect(prompt).toContain('mini-pc');
    expect(prompt).toContain('RTX 3060 12GB');
    expect(prompt).toContain('A100 80GB');
    expect(prompt).toContain('No models loaded'); // mini-pc has no models
    expect(prompt).toContain('Devices: 3');
  });

  it('should include VRAM tier model recommendations', () => {
    // Arrange
    const ctx = { userName: 'Dave', plan: 'free', devices: [] };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('qwen3:8b');
    expect(prompt).toContain('llama3.3:70b');
    expect(prompt).toContain('qwen3:1.7b');
    expect(prompt).toContain('qwen3:14b');
    expect(prompt).toContain('qwen3:32b');
    expect(prompt).toContain('VRAM');
  });

  it('should use fallback text for empty hardware', () => {
    // Arrange
    const ctx = {
      userName: 'Eve',
      plan: 'free',
      devices: [
        {
          name: 'unknown-box',
          status: 'online',
          models: ['qwen3:4b'],
          hardware: '',
          tunnelUrl: 'https://box.llmhosts.com',
        },
      ],
    };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('Hardware unknown');
  });

  it('should include userName and plan in user context section', () => {
    // Arrange
    const ctx = {
      userName: 'Frank',
      plan: 'enterprise',
      devices: [],
    };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('Name: Frank');
    expect(prompt).toContain('Plan: enterprise');
    expect(prompt).toContain('Current User');
  });

  it('should use "Unknown" fallback when userName is empty', () => {
    // Arrange
    const ctx = { userName: '', plan: 'free', devices: [] };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('Name: Unknown');
  });

  it('should contain the base personality and capabilities', () => {
    // Arrange
    const ctx = { userName: 'Grace', plan: 'free', devices: [] };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('You are Token');
    expect(prompt).toContain('LLMHosts.com');
    expect(prompt).toContain('Model Recommendations');
    expect(prompt).toContain('Fleet Overview');
    expect(prompt).toContain('Cost Analysis');
    expect(prompt).toContain('Troubleshooting');
  });

  it('should show "No models loaded" for a device with empty models array', () => {
    // Arrange
    const ctx = {
      userName: 'Hank',
      plan: 'pro',
      devices: [
        {
          name: 'empty-device',
          status: 'online',
          models: [],
          hardware: 'GTX 1080',
          tunnelUrl: 'https://empty.llmhosts.com',
        },
      ],
    };

    // Act
    const prompt = buildSystemPrompt(ctx);

    // Assert
    expect(prompt).toContain('No models loaded');
  });
});
