/**
 * DNS Route Tests
 *
 * Tests DNS record management endpoints (create, delete, resolve)
 * Note: All DNS routes require a token. We pass token='test-token' to bypass
 * credential lookup (requireDnsToken returns providedToken if truthy).
 */

const request = require('supertest');
const fs = require('fs');
const path = require('path');
const os = require('os');

const testServicesFile = path.join(os.tmpdir(), `dns-services-${Date.now()}.json`);
const testConfigFile = path.join(os.tmpdir(), `dns-config-${Date.now()}.json`);

process.env.SERVICES_FILE = testServicesFile;
process.env.CONFIG_FILE = testConfigFile;
process.env.ENABLE_HEALTH_CHECKER = 'false';
process.env.NODE_ENV = 'test';

fs.writeFileSync(testServicesFile, '[]', 'utf8');
fs.writeFileSync(testConfigFile, '{}', 'utf8');

const app = require('../server');

describe('DNS Routes', () => {
  afterAll(() => {
    try { fs.unlinkSync(testServicesFile); } catch (e) { /* ignore */ }
    try { fs.unlinkSync(testConfigFile); } catch (e) { /* ignore */ }
  });

  describe('POST /api/dns/record', () => {
    test('should reject missing domain', async () => {
      const res = await request(app)
        .post('/api/dns/record')
        .send({ ip: '192.168.1.1', token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.success).toBe(false);
      expect(res.body.error).toContain('domain');
    });

    test('should reject missing ip', async () => {
      const res = await request(app)
        .post('/api/dns/record')
        .send({ domain: 'test.sami', token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.success).toBe(false);
    });

    test('should reject invalid domain format', async () => {
      const res = await request(app)
        .post('/api/dns/record')
        .send({ domain: '!!!invalid!!!', ip: '192.168.1.1', token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.error).toContain('Invalid domain');
    });

    test('should reject invalid IP address', async () => {
      const res = await request(app)
        .post('/api/dns/record')
        .send({ domain: 'test.sami', ip: 'not-an-ip', token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.error).toContain('Invalid IP');
    });

    test('should reject invalid TTL', async () => {
      const res = await request(app)
        .post('/api/dns/record')
        .send({ domain: 'test.sami', ip: '192.168.1.1', ttl: 10, token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.error).toContain('TTL');
    });
  });

  describe('DELETE /api/dns/record', () => {
    test('should reject missing domain', async () => {
      const res = await request(app)
        .delete('/api/dns/record')
        .query({ token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.error).toContain('domain');
    });

    test('should reject invalid domain format', async () => {
      const res = await request(app)
        .delete('/api/dns/record')
        .query({ domain: '!!!bad!!!', token: 'test-token' });

      expect(res.statusCode).toBe(400);
    });

    test('should reject invalid record type', async () => {
      const res = await request(app)
        .delete('/api/dns/record')
        .query({ domain: 'test.sami', type: 'INVALID', token: 'test-token' });

      expect(res.statusCode).toBe(400);
      expect(res.body.error).toContain('Invalid DNS record type');
    });

    test('should reject invalid IP address in query', async () => {
      const res = await request(app)
        .delete('/api/dns/record')
        .query({ domain: 'test.sami', ipAddress: 'not-ip', token: 'test-token' });

      expect(res.statusCode).toBe(400);
    });

    test('should reject invalid server address', async () => {
      const res = await request(app)
        .delete('/api/dns/record')
        .query({ domain: 'test.sami', server: 'not-ip', token: 'test-token' });

      expect(res.statusCode).toBe(400);
    });
  });

  describe('GET /api/dns/resolve', () => {
    test('should reject missing domain', async () => {
      const res = await request(app)
        .get('/api/dns/resolve')
        .query({ token: 'test-token' });

      expect(res.statusCode).toBe(400);
    });

    test('should reject invalid domain format', async () => {
      const res = await request(app)
        .get('/api/dns/resolve')
        .query({ domain: '!!!bad!!!', token: 'test-token' });

      expect(res.statusCode).toBe(400);
    });
  });
});
