import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';

describe('Main Bootstrap', () => {
  it('should have AppModule defined', () => {
    expect(AppModule).toBeDefined();
  });

  it('should configure ValidationPipe with correct options', () => {
    const validationPipe = new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: false,
      transform: true,
    });

    expect(validationPipe).toBeDefined();
  });

  it('should handle CORS configuration for development', () => {
    const mockCallback = jest.fn();

    // Simula a função de CORS para desenvolvimento
    const corsOrigin = (origin: string, callback: any) => {
      if (process.env.NODE_ENV === 'development') {
        return callback(null, true);
      }
      if (!origin) return callback(null, true);

      const allowedOrigins = [
        'http://localhost:3000',
        'https://localhost:3000',
        'https://dev.app.planfi.com.br',
        'https://planfi.com.br',
        'https://api.planfi.com.br',
        'https://app.planfi.com.br',
        'https://atlas.planfi.com.br',
      ];

      const isSubdomainOfPlanfiNet = /^https:\/\/.*\.planfi\.net$/.test(origin);
      if (allowedOrigins.includes(origin) || isSubdomainOfPlanfiNet) {
        return callback(null, true);
      }

      return callback(new Error('Not allowed by CORS'));
    };

    // Testa origem permitida
    corsOrigin('https://app.planfi.com.br', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(null, true);

    // Testa subdomínio permitido
    mockCallback.mockClear();
    corsOrigin('https://test.planfi.net', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(null, true);

    // Testa origem não permitida
    mockCallback.mockClear();
    corsOrigin('https://malicious.com', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(expect.any(Error));
  });

  it('should handle CORS configuration for production', () => {
    const originalEnv = process.env.NODE_ENV;
    process.env.NODE_ENV = 'production';

    const mockCallback = jest.fn();

    const corsOrigin = (origin: string, callback: any) => {
      if (process.env.NODE_ENV === 'development') {
        return callback(null, true);
      }
      if (!origin) return callback(null, true);

      const allowedOrigins = [
        'http://localhost:3000',
        'https://localhost:3000',
        'https://dev.app.planfi.com.br',
        'https://planfi.com.br',
        'https://api.planfi.com.br',
        'https://app.planfi.com.br',
        'https://atlas.planfi.com.br',
      ];

      const isSubdomainOfPlanfiNet = /^https:\/\/.*\.planfi\.net$/.test(origin);
      if (allowedOrigins.includes(origin) || isSubdomainOfPlanfiNet) {
        return callback(null, true);
      }

      return callback(new Error('Not allowed by CORS'));
    };

    // Testa origem permitida em produção
    corsOrigin('https://app.planfi.com.br', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(null, true);

    // Testa origem não permitida em produção
    mockCallback.mockClear();
    corsOrigin('https://malicious.com', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(expect.any(Error));

    // Restaura o ambiente original
    process.env.NODE_ENV = originalEnv;
  });

  it('should handle requests without origin', () => {
    const mockCallback = jest.fn();

    const corsOrigin = (origin: string, callback: any) => {
      if (process.env.NODE_ENV === 'development') {
        return callback(null, true);
      }
      if (!origin) return callback(null, true);

      const allowedOrigins = [
        'http://localhost:3000',
        'https://localhost:3000',
        'https://dev.app.planfi.com.br',
        'https://planfi.com.br',
        'https://api.planfi.com.br',
        'https://app.planfi.com.br',
        'https://atlas.planfi.com.br',
      ];

      const isSubdomainOfPlanfiNet = /^https:\/\/.*\.planfi\.net$/.test(origin);
      if (allowedOrigins.includes(origin) || isSubdomainOfPlanfiNet) {
        return callback(null, true);
      }

      return callback(new Error('Not allowed by CORS'));
    };

    // Testa requisição sem origin (ex: curl)
    corsOrigin('', mockCallback);
    expect(mockCallback).toHaveBeenCalledWith(null, true);
  });

  it('should validate allowed origins list', () => {
    const allowedOrigins = [
      'http://localhost:3000',
      'https://localhost:3000',
      'https://dev.app.planfi.com.br',
      'https://planfi.com.br',
      'https://api.planfi.com.br',
      'https://app.planfi.com.br',
      'https://atlas.planfi.com.br',
    ];

    expect(allowedOrigins).toHaveLength(7);
    expect(allowedOrigins).toContain('https://app.planfi.com.br');
    expect(allowedOrigins).toContain('https://planfi.com.br');
  });

  it('should validate subdomain regex pattern', () => {
    const isSubdomainOfPlanfiNet = /^https:\/\/.*\.planfi\.net$/;

    expect(isSubdomainOfPlanfiNet.test('https://test.planfi.net')).toBe(true);
    expect(isSubdomainOfPlanfiNet.test('https://api.planfi.net')).toBe(true);
    expect(isSubdomainOfPlanfiNet.test('https://planfi.net')).toBe(false);
    expect(
      isSubdomainOfPlanfiNet.test('https://malicious.planfi.net.evil.com'),
    ).toBe(false);
    expect(isSubdomainOfPlanfiNet.test('http://test.planfi.net')).toBe(false);
  });

  it('should handle port configuration', () => {
    const port = process.env.PORT ?? 3333;
    expect(port).toBeDefined();
    expect(['string', 'number']).toContain(typeof port);
  });
});
