/**
 * H5P Service Integration Tests (ESM)
 * Tests für alle H5P Service Endpoints
 */

import request from 'supertest';
import { describe, test, expect } from '@jest/globals';
import { app } from '../server-simple.js';

describe('H5P Service - Health & Info', () => {
  test('GET / should return service info page', async () => {
    const response = await request(app).get('/');

    expect(response.status).toBe(200);
    expect(response.text).toContain('H5P Service');
    expect(response.text).toContain('Running');
  });

  test('GET /health should return healthy status', async () => {
    const response = await request(app).get('/health');

    expect(response.status).toBe(200);
    expect(response.body).toMatchObject({
      status: 'healthy',
      service: 'h5p-service-simplified'
    });
  });
});

describe('H5P Service - Editor Selection Page', () => {
  test('GET /h5p/editor/new should return editor selection page', async () => {
    const response = await request(app).get('/h5p/editor/new');

    expect(response.status).toBe(200);
    expect(response.text).toContain('H5P Content Creator');
    expect(response.text).toContain('Quiz');
    expect(response.text).toContain('Interactive Video');
    expect(response.text).toContain('Course Presentation');
    expect(response.text).toContain('Flashcards');
    expect(response.text).toContain('Timeline');
    expect(response.text).toContain('Drag and Drop');
    expect(response.text).toContain('Fill in the Blanks');
    expect(response.text).toContain('Memory Game');
  });

  test('Editor page should contain all 8 content types', async () => {
    const response = await request(app).get('/h5p/editor/new');

    const contentTypes = [
      'Quiz',
      'Interactive Video',
      'Course Presentation',
      'Flashcards',
      'Timeline',
      'Drag and Drop',
      'Fill in the Blanks',
      'Memory Game'
    ];

    contentTypes.forEach(type => {
      expect(response.text).toContain(type);
    });
  });
});

describe('H5P Service - Quiz Editor & Player', () => {
  test('GET /h5p/editor/quiz should return quiz editor', async () => {
    const response = await request(app).get('/h5p/editor/quiz');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Quiz Editor');
    expect(response.text).toContain('Frage hinzufügen');
  });

  test('GET /h5p/player/quiz should return quiz player', async () => {
    const response = await request(app).get('/h5p/player/quiz');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Quiz');
  });
});

describe('H5P Service - Interactive Video Editor & Player', () => {
  test('GET /h5p/editor/interactive-video should return video editor', async () => {
    const response = await request(app).get('/h5p/editor/interactive-video');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Interactive Video Editor');
    expect(response.text).toContain('Video-URL');
  });

  test('GET /h5p/player/interactive-video should return video player', async () => {
    const response = await request(app).get('/h5p/player/interactive-video');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Interactive Video');
  });
});

describe('H5P Service - Course Presentation Editor & Player', () => {
  test('GET /h5p/editor/course-presentation should return presentation editor', async () => {
    const response = await request(app).get('/h5p/editor/course-presentation');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Course Presentation Editor');
    expect(response.text).toContain('Folien');
  });

  test('GET /h5p/player/course-presentation should return presentation player', async () => {
    const response = await request(app).get('/h5p/player/course-presentation');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Course Presentation');
  });
});

describe('H5P Service - Flashcards Editor', () => {
  test('GET /h5p/editor/flashcards should return flashcards editor', async () => {
    const response = await request(app).get('/h5p/editor/flashcards');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Flashcards Editor');
    expect(response.text).toContain('Karte hinzufügen');
  });
});

describe('H5P Service - Timeline Editor & Player', () => {
  test('GET /h5p/editor/timeline should return timeline editor', async () => {
    const response = await request(app).get('/h5p/editor/timeline');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Timeline Editor');
    expect(response.text).toContain('Ereignis');
  });

  test('GET /h5p/player/timeline should return timeline player', async () => {
    const response = await request(app).get('/h5p/player/timeline');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Timeline');
  });
});

describe('H5P Service - Drag and Drop Editor & Player', () => {
  test('GET /h5p/editor/drag-drop should return drag drop editor', async () => {
    const response = await request(app).get('/h5p/editor/drag-drop');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Drag and Drop Editor');
    expect(response.text).toContain('Drop Zones');
  });

  test('GET /h5p/player/drag-drop should return drag drop player', async () => {
    const response = await request(app).get('/h5p/player/drag-drop');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Drag and Drop');
  });
});

describe('H5P Service - Fill in the Blanks Editor & Player', () => {
  test('GET /h5p/editor/fill-blanks should return fill blanks editor', async () => {
    const response = await request(app).get('/h5p/editor/fill-blanks');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Fill in the Blanks Editor');
    expect(response.text).toContain('Lückentext');
  });

  test('GET /h5p/player/fill-blanks should return fill blanks player', async () => {
    const response = await request(app).get('/h5p/player/fill-blanks');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Fill in the Blanks');
  });
});

describe('H5P Service - Memory Game Editor & Player', () => {
  test('GET /h5p/editor/memory should return memory editor', async () => {
    const response = await request(app).get('/h5p/editor/memory');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Memory Game Editor');
    expect(response.text).toContain('Paar');
  });

  test('GET /h5p/player/memory should return memory player', async () => {
    const response = await request(app).get('/h5p/player/memory');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Memory');
  });
});

describe('H5P Service - Static Files', () => {
  test('Static core files should be accessible', async () => {
    const response = await request(app).get('/h5p/core/h5p.css');

    // May or may not exist, but should not 500
    expect([200, 404]).toContain(response.status);
  });

  test('Editors directory should be accessible', async () => {
    const response = await request(app).get('/h5p/editors/quiz-editor.html');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Quiz Editor');
  });

  test('Players directory should be accessible', async () => {
    const response = await request(app).get('/h5p/players/quiz-player.html');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Quiz');
  });
});

describe('H5P Service - Error Handling', () => {
  test('GET /nonexistent should return 404', async () => {
    const response = await request(app).get('/nonexistent');

    expect(response.status).toBe(404);
  });

  test('Invalid editor route should return 404', async () => {
    const response = await request(app).get('/h5p/editor/nonexistent');

    expect(response.status).toBe(404);
  });

  test('Invalid player route should return 404', async () => {
    const response = await request(app).get('/h5p/player/nonexistent');

    expect(response.status).toBe(404);
  });
});
