import { Test, TestingModule } from '@nestjs/testing';
import { AISafetyService } from './ai-safety.service';

describe('AISafetyService', () => {
  let service: AISafetyService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [AISafetyService],
    }).compile();

    service = module.get<AISafetyService>(AISafetyService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  describe('checkInputSafety', () => {
    describe('Emergency Keywords', () => {
      it('should detect emergency keyword "not breathing"', () => {
        const result = service.checkInputSafety(
          'My baby is not breathing',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('emergency');
        expect(result.keywords).toContain('not breathing');
        expect(result.recommendedResponse).toContain('911');
      });

      it('should detect emergency keyword "choking"', () => {
        const result = service.checkInputSafety(
          'My child is choking',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('emergency');
        expect(result.keywords).toContain('choking');
      });

      it('should detect emergency keyword "seizure"', () => {
        const result = service.checkInputSafety(
          'Baby having a seizure',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('emergency');
        expect(result.keywords).toContain('seizure');
      });
    });

    describe('Crisis Keywords', () => {
      it('should detect crisis keyword "suicide"', () => {
        const result = service.checkInputSafety(
          'I am thinking about suicide',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('crisis');
        expect(result.keywords).toContain('suicide');
        expect(result.recommendedResponse).toContain('988');
      });

      it('should detect crisis keyword "postpartum depression"', () => {
        const result = service.checkInputSafety(
          'I think I have postpartum depression',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('crisis');
        expect(result.keywords).toContain('postpartum depression');
      });

      it('should detect crisis keyword "hurt myself"', () => {
        const result = service.checkInputSafety(
          'I want to hurt myself',
          'test-user',
        );

        expect(result.isSafe).toBe(false);
        expect(result.trigger).toBe('crisis');
        expect(result.keywords).toContain('hurt myself');
      });
    });

    describe('Medical Keywords', () => {
      it('should detect medical keyword "fever"', () => {
        const result = service.checkInputSafety(
          'My baby has a fever',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('medical');
        expect(result.keywords).toContain('fever');
        expect(result.requiresDisclaimer).toBe(true);
      });

      it('should detect medical keyword "vomiting"', () => {
        const result = service.checkInputSafety(
          'My child is vomiting',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('medical');
        expect(result.keywords).toContain('vomiting');
      });

      it('should detect medical keyword "rash"', () => {
        const result = service.checkInputSafety(
          'Baby has a rash on his arm',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('medical');
        expect(result.keywords).toContain('rash');
      });
    });

    describe('Developmental Keywords', () => {
      it('should detect developmental keyword "delay"', () => {
        const result = service.checkInputSafety(
          'My child has a speech delay',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('developmental');
        expect(result.keywords).toContain('delay');
      });

      it('should detect developmental keyword "autism"', () => {
        const result = service.checkInputSafety(
          'Could my child have autism',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('developmental');
        expect(result.keywords).toContain('autism');
      });
    });

    describe('Stress Keywords', () => {
      it('should detect stress keyword "overwhelmed"', () => {
        const result = service.checkInputSafety(
          'I feel so overwhelmed',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('stress');
        expect(result.keywords).toContain('overwhelmed');
      });

      it('should detect stress keyword "burnout"', () => {
        const result = service.checkInputSafety(
          'I have burnout from parenting',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBe('stress');
        expect(result.keywords).toContain('burnout');
      });
    });

    describe('Safe Queries', () => {
      it('should allow safe parenting question', () => {
        const result = service.checkInputSafety(
          'What time should my baby go to bed?',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBeUndefined();
        expect(result.requiresDisclaimer).toBe(false);
      });

      it('should allow routine question', () => {
        const result = service.checkInputSafety(
          'How can I establish a bedtime routine?',
          'test-user',
        );

        expect(result.isSafe).toBe(true);
        expect(result.trigger).toBeUndefined();
      });
    });
  });

  describe('checkOutputSafety', () => {
    it('should detect dosage pattern in output', () => {
      const result = service.checkOutputSafety(
        'Give the baby 5 ml of acetaminophen every 4 hours',
      );

      expect(result.isSafe).toBe(false);
      expect(result.trigger).toBe('content_filter');
    });

    it('should detect diagnostic language', () => {
      const result = service.checkOutputSafety(
        'Your child definitely has an ear infection',
      );

      expect(result.isSafe).toBe(false);
      expect(result.trigger).toBe('content_filter');
    });

    it('should allow safe parenting advice', () => {
      const result = service.checkOutputSafety(
        'Establishing a bedtime routine can help your baby sleep better',
      );

      expect(result.isSafe).toBe(true);
    });
  });

  describe('getEmergencyResponse', () => {
    it('should include 911 and emergency instructions', () => {
      const response = service.getEmergencyResponse();

      expect(response).toContain('911');
      expect(response).toContain('EMERGENCY');
      expect(response).toContain('Poison Control');
      expect(response).toContain('1-800-222-1222');
    });
  });

  describe('getCrisisResponse', () => {
    it('should include crisis hotline numbers', () => {
      const response = service.getCrisisResponse();

      expect(response).toContain('988');
      expect(response).toContain('Postpartum Support International');
      expect(response).toContain('1-800-944-4773');
      expect(response).toContain('741741');
      expect(response).toContain('1-800-422-4453');
    });
  });

  describe('getMedicalDisclaimer', () => {
    it('should include medical disclaimer warning', () => {
      const disclaimer = service.getMedicalDisclaimer();

      expect(disclaimer).toContain('Medical Disclaimer');
      expect(disclaimer).toContain('not a medical professional');
      expect(disclaimer).toContain('pediatrician');
    });

    it('should include when to seek immediate care', () => {
      const disclaimer = service.getMedicalDisclaimer();

      expect(disclaimer).toContain('When to seek immediate care');
      expect(disclaimer).toContain('fever');
      expect(disclaimer).toContain('breathing');
    });
  });

  describe('getStressSupport', () => {
    it('should include stress support resources', () => {
      const support = service.getStressSupport();

      expect(support).toContain('Postpartum Support International');
      expect(support).toContain('Parents Anonymous');
      expect(support).toContain('1-855-427-2736');
      expect(support).toContain('Self-Care Reminders');
    });
  });

  describe('injectSafetyResponse', () => {
    it('should inject medical disclaimer for medical trigger', () => {
      const aiResponse = 'Here is some advice about fever';
      const result = service.injectSafetyResponse('medical', aiResponse, [
        'fever',
      ]);

      expect(result).toContain('Medical Disclaimer');
      expect(result).toContain(aiResponse);
    });

    it('should inject stress support for stress trigger', () => {
      const aiResponse = 'Here is some advice';
      const result = service.injectSafetyResponse('stress', aiResponse, [
        'overwhelmed',
      ]);

      expect(result).toContain('Parenting is Hard');
      expect(result).toContain(aiResponse);
    });

    it('should return crisis response for crisis trigger', () => {
      const result = service.injectSafetyResponse('crisis', '', ['suicide']);

      expect(result).toContain('CRISIS SUPPORT');
      expect(result).toContain('988');
    });
  });

  describe('getBaseSafetyPrompt', () => {
    it('should include critical safety rules', () => {
      const prompt = service.getBaseSafetyPrompt();

      expect(prompt).toContain('NOT a medical professional');
      expect(prompt).toContain('911');
      expect(prompt).toContain('crisis hotline');
      expect(prompt).toContain('ages 0-6 years');
    });

    it('should specify out of scope items', () => {
      const prompt = service.getBaseSafetyPrompt();

      expect(prompt).toContain('OUT OF SCOPE');
      expect(prompt).toContain('Medical diagnosis');
      expect(prompt).toContain('Legal advice');
    });
  });

  describe('Safety Overrides', () => {
    it('should provide medical safety override', () => {
      const override = service.getMedicalSafetyOverride();

      expect(override).toContain('MEDICAL SAFETY OVERRIDE');
      expect(override).toContain('medical disclaimer');
    });

    it('should provide crisis safety override', () => {
      const override = service.getCrisisSafetyOverride();

      expect(override).toContain('CRISIS RESPONSE OVERRIDE');
      expect(override).toContain('crisis hotline');
    });
  });
});
