import { afterEach, describe, expect, it, vi } from 'vitest';
import {
  fetchGraph,
  getBackendUrl,
  normalizeServerUrl,
  setBackendUrl,
  validateBackendUrl,
} from '../../src/services/backend-client';

describe('normalizeServerUrl', () => {
  it('adds http:// to localhost', () => {
    expect(normalizeServerUrl('localhost:4747')).toBe('http://localhost:4747');
  });

  it('adds http:// to 127.0.0.1', () => {
    expect(normalizeServerUrl('127.0.0.1:4747')).toBe('http://127.0.0.1:4747');
  });

  it('adds https:// to non-local hosts', () => {
    expect(normalizeServerUrl('example.com')).toBe('https://example.com');
  });

  it('strips trailing slashes', () => {
    expect(normalizeServerUrl('http://localhost:4747/')).toBe('http://localhost:4747');
    expect(normalizeServerUrl('http://localhost:4747///')).toBe('http://localhost:4747');
  });

  it('strips /api suffix (base URL only)', () => {
    expect(normalizeServerUrl('http://localhost:4747/api')).toBe('http://localhost:4747');
  });

  it('trims whitespace', () => {
    expect(normalizeServerUrl('  localhost:4747  ')).toBe('http://localhost:4747');
  });

  it('preserves existing https://', () => {
    expect(normalizeServerUrl('https://gitnexus.example.com')).toBe('https://gitnexus.example.com');
  });
});

afterEach(() => {
  vi.restoreAllMocks();
});

describe('fetchGraph', () => {
  it('requests streamed graph responses from the backend', async () => {
    setBackendUrl('http://localhost:4747');

    const fetchMock = vi.fn().mockResolvedValue(
      new Response('{"nodes":[],"relationships":[]}', {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        },
      }),
    );
    vi.stubGlobal('fetch', fetchMock);

    await fetchGraph('big-repo');

    expect(fetchMock).toHaveBeenCalledWith(
      expect.stringContaining('/api/graph?repo=big-repo&stream=true'),
      expect.any(Object),
    );
  });

  it('parses NDJSON graph streams incrementally', async () => {
    setBackendUrl('http://localhost:4747');

    const encoder = new TextEncoder();
    const stream = new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(
          encoder.encode(
            [
              '{"type":"node","data":{"id":"File:src/app.ts","label":"File","properties":{"name":"app.ts","filePath":"src/app.ts"}}}\n',
              '{"type":"relationship","data":{"id":"File:src/app.ts_CONTAINS_Function:src/app.ts:main","type":"CONTAINS","sourceId":"File:src/app.ts","targetId":"Function:src/app.ts:main"}}\n',
            ].join(''),
          ),
        );
        controller.close();
      },
    });

    vi.stubGlobal(
      'fetch',
      vi.fn().mockResolvedValue(
        new Response(stream, {
          status: 200,
          headers: {
            'Content-Type': 'application/x-ndjson',
          },
        }),
      ),
    );

    const progress = vi.fn();
    const result = await fetchGraph('big-repo', { onProgress: progress });

    expect(result.nodes).toHaveLength(1);
    expect(result.relationships).toHaveLength(1);
    expect(result.nodes[0].id).toBe('File:src/app.ts');
    expect(result.relationships[0].type).toBe('CONTAINS');
    expect(progress).toHaveBeenCalled();
  });

  it('parses NDJSON graph lines split across chunks', async () => {
    setBackendUrl('http://localhost:4747');

    const encoder = new TextEncoder();
    const stream = new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(
          encoder.encode(
            '{"type":"node","data":{"id":"File:src/app.ts","label":"File","properties":{"name":"app.ts"',
          ),
        );
        controller.enqueue(
          encoder.encode(
            ',"filePath":"src/app.ts"}}}\n{"type":"relationship","data":{"id":"File:src/app.ts_CONTAINS_Function:src/app.ts:main","type":"CONTAINS","sourceId":"File:src/app.ts","targetId":"Function:src/app.ts:main"}}\n',
          ),
        );
        controller.close();
      },
    });

    vi.stubGlobal(
      'fetch',
      vi.fn().mockResolvedValue(
        new Response(stream, {
          status: 200,
          headers: {
            'Content-Type': 'application/x-ndjson',
          },
        }),
      ),
    );

    const result = await fetchGraph('big-repo');

    expect(result.nodes).toHaveLength(1);
    expect(result.relationships).toHaveLength(1);
    expect(result.nodes[0].properties.filePath).toBe('src/app.ts');
  });

  it('throws backend errors emitted in the NDJSON stream', async () => {
    setBackendUrl('http://localhost:4747');

    const encoder = new TextEncoder();
    const stream = new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(encoder.encode('{"type":"error","error":"stream failed"}\n'));
        controller.close();
      },
    });

    vi.stubGlobal(
      'fetch',
      vi.fn().mockResolvedValue(
        new Response(stream, {
          status: 200,
          headers: {
            'Content-Type': 'application/x-ndjson',
          },
        }),
      ),
    );

    await expect(fetchGraph('big-repo')).rejects.toMatchObject({
      message: 'stream failed',
    });
  });
});

describe('DEFAULT_BACKEND_URL resolution', () => {
  afterEach(() => {
    delete window.__GITNEXUS_CONFIG__;
    vi.resetModules();
  });

  it('falls back to localhost:4747 when no config is injected', async () => {
    delete window.__GITNEXUS_CONFIG__;
    const { DEFAULT_BACKEND_URL } = await import('../../src/config/ui-constants');
    expect(DEFAULT_BACKEND_URL).toBe('http://localhost:4747');
  });

  it('uses window.__GITNEXUS_CONFIG__.backendUrl when set', async () => {
    window.__GITNEXUS_CONFIG__ = { backendUrl: 'http://10.0.0.1:4747' };
    const { DEFAULT_BACKEND_URL } = await import('../../src/config/ui-constants');
    expect(DEFAULT_BACKEND_URL).toBe('http://10.0.0.1:4747');
  });

  it('falls back to localhost:4747 when config object has no backendUrl', async () => {
    window.__GITNEXUS_CONFIG__ = {};
    const { DEFAULT_BACKEND_URL } = await import('../../src/config/ui-constants');
    expect(DEFAULT_BACKEND_URL).toBe('http://localhost:4747');
  });

  it('falls back to localhost:4747 when backendUrl is an empty string', async () => {
    window.__GITNEXUS_CONFIG__ = { backendUrl: '' };
    const { DEFAULT_BACKEND_URL } = await import('../../src/config/ui-constants');
    expect(DEFAULT_BACKEND_URL).toBe('http://localhost:4747');
  });
});

describe('validateBackendUrl', () => {
  it('allows http:// URLs', () => {
    expect(() => validateBackendUrl('http://localhost:4747')).not.toThrow();
    expect(() => validateBackendUrl('http://127.0.0.1:4747')).not.toThrow();
  });

  it('allows https:// URLs', () => {
    expect(() => validateBackendUrl('https://gitnexus.example.com')).not.toThrow();
    expect(() => validateBackendUrl('https://my-server.internal:4747')).not.toThrow();
  });

  it('rejects non-http schemes', () => {
    expect(() => validateBackendUrl('javascript:alert(1)')).toThrow('must use http:// or https://');
    expect(() => validateBackendUrl('file:///etc/passwd')).toThrow('must use http:// or https://');
    expect(() => validateBackendUrl('data:text/plain,evil')).toThrow(
      'must use http:// or https://',
    );
  });

  it('rejects malformed URLs', () => {
    expect(() => validateBackendUrl('not-a-url')).toThrow('Invalid backend URL');
  });

  it('does not include the raw URL in error messages (credential hygiene)', () => {
    const urlWithCreds = 'javascript:alert("sk-secret")';
    let msg = '';
    try {
      validateBackendUrl(urlWithCreds);
    } catch (e) {
      msg = (e as Error).message;
    }
    expect(msg).not.toContain('sk-secret');
    expect(msg).not.toContain(urlWithCreds);
  });
});

describe('setBackendUrl', () => {
  it('accepts valid http URLs', () => {
    expect(() => setBackendUrl('http://localhost:4747')).not.toThrow();
  });

  it('accepts valid https URLs', () => {
    expect(() => setBackendUrl('https://my-server.example.com')).not.toThrow();
  });

  it('rejects non-http/https schemes', () => {
    expect(() => setBackendUrl('javascript:alert(1)')).toThrow('must use http:// or https://');
    expect(() => setBackendUrl('file:///etc/passwd')).toThrow('must use http:// or https://');
  });

  it('does not mutate _backendUrl when validation fails', () => {
    setBackendUrl('http://localhost:4747');
    expect(() => setBackendUrl('javascript:alert(1)')).toThrow();
    expect(getBackendUrl()).toBe('http://localhost:4747');
  });
});
