import { describe, it, expect, beforeEach } from 'vitest';
import fs from 'fs';

import Database from 'better-sqlite3';

/**
 * Tests for the environment check step.
 *
 * Verifies: config detection, Docker/AC detection, DB queries.
 */

describe('environment detection', () => {
 it('detects platform correctly', async () => {
    const { getPlatform } = await import('./platform.js');
    const platform = getPlatform();
    expect(['freebsd', 'unknown']).toContain(platform);
  });
});

describe('registered groups DB query', () => {
  let db: Database.Database;

  beforeEach(() => {
    db = new Database(':memory:');
    db.exec(`CREATE TABLE IF NOT EXISTS registered_groups (
      jid TEXT PRIMARY KEY,
      name TEXT NOT NULL,
      folder TEXT NOT NULL UNIQUE,
      trigger_pattern TEXT NOT NULL,
      added_at TEXT NOT NULL,
      jail_config TEXT,
      requires_trigger INTEGER DEFAULT 1
    )`);
  });

  it('returns 0 for empty table', () => {
    const row = db
      .prepare('SELECT COUNT(*) as count FROM registered_groups')
      .get() as { count: number };
    expect(row.count).toBe(0);
  });

  it('returns correct count after inserts', () => {
    db.prepare(
      `INSERT INTO registered_groups (jid, name, folder, trigger_pattern, added_at, requires_trigger)
       VALUES (?, ?, ?, ?, ?, ?)`,
    ).run(
      '123@g.us',
      'Group 1',
      'group-1',
      '@Andy',
      '2024-01-01T00:00:00.000Z',
      1,
    );

    db.prepare(
      `INSERT INTO registered_groups (jid, name, folder, trigger_pattern, added_at, requires_trigger)
       VALUES (?, ?, ?, ?, ?, ?)`,
    ).run(
      '456@g.us',
      'Group 2',
      'group-2',
      '@Andy',
      '2024-01-01T00:00:00.000Z',
      1,
    );

    const row = db
      .prepare('SELECT COUNT(*) as count FROM registered_groups')
      .get() as { count: number };
    expect(row.count).toBe(2);
  });
});

describe('credentials detection', () => {
  it('detects ANTHROPIC_API_KEY in env content', () => {
    const content =
      'SOME_KEY=value\nANTHROPIC_API_KEY=sk-ant-test123\nOTHER=foo';
    const hasCredentials =
      /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(content);
    expect(hasCredentials).toBe(true);
  });

  it('detects OPENROUTER_API_KEY in env content', () => {
    const content = 'OPENROUTER_API_KEY=sk-or-test123';
    const hasCredentials =
      /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(content);
    expect(hasCredentials).toBe(true);
  });

  it('returns false when no credentials', () => {
    const content = 'ASSISTANT_NAME="Andy"\nOTHER=foo';
    const hasCredentials =
      /^(ANTHROPIC_API_KEY|OPENROUTER_API_KEY)=/m.test(content);
    expect(hasCredentials).toBe(false);
  });
});

describe('command detection logic', () => {
  it('commandExists returns boolean', async () => {
    const { commandExists } = await import('./platform.js');
    expect(typeof commandExists('jexec')).toBe('boolean');
    expect(typeof commandExists('nonexistent_binary_xyz')).toBe('boolean');
  });
});

describe('Telegram auth detection', () => {
  it('detects TELEGRAM_BOT_TOKEN in env content', () => {
    const content = 'TELEGRAM_BOT_TOKEN=123456:token';
    const configured = /^TELEGRAM_BOT_TOKEN=.+/m.test(content);
    expect(configured).toBe(true);
  });

  it('returns false when TELEGRAM_BOT_TOKEN is missing', () => {
    const content = 'ASSISTANT_NAME="Andy"\nOTHER=foo';
    const configured = /^TELEGRAM_BOT_TOKEN=.+/m.test(content);
    expect(configured).toBe(false);
  });
});
