/* eslint-disable */
'use strict';

module.exports = {
  async up(queryInterface) {
    // Grammatik-Übungstypen (z.B. "gap_fill", "multiple_choice", "sentence_building", "transformation")
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_grammar_exercise_type (
        id SERIAL PRIMARY KEY,
        name TEXT NOT NULL UNIQUE,
        description TEXT,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW()
      );
    `);

    // Grammatik-Übungen (verknüpft mit Lektionen)
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_grammar_exercise (
        id SERIAL PRIMARY KEY,
        lesson_id INTEGER NOT NULL,
        exercise_type_id INTEGER NOT NULL,
        exercise_number INTEGER NOT NULL,
        title TEXT NOT NULL,
        instruction TEXT,
        question_data JSONB NOT NULL,
        answer_data JSONB NOT NULL,
        explanation TEXT,
        created_by_user_id INTEGER NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_grammar_exercise_lesson_fk
          FOREIGN KEY (lesson_id)
          REFERENCES community.vocab_course_lesson(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_grammar_exercise_type_fk
          FOREIGN KEY (exercise_type_id)
          REFERENCES community.vocab_grammar_exercise_type(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_grammar_exercise_creator_fk
          FOREIGN KEY (created_by_user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_grammar_exercise_unique UNIQUE (lesson_id, exercise_number)
      );
    `);

    // Fortschritt für Grammatik-Übungen
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_grammar_exercise_progress (
        id SERIAL PRIMARY KEY,
        user_id INTEGER NOT NULL,
        exercise_id INTEGER NOT NULL,
        attempts INTEGER DEFAULT 0,
        correct_attempts INTEGER DEFAULT 0,
        last_attempt_at TIMESTAMP WITHOUT TIME ZONE,
        completed BOOLEAN DEFAULT false,
        completed_at TIMESTAMP WITHOUT TIME ZONE,
        CONSTRAINT vocab_grammar_exercise_progress_user_fk
          FOREIGN KEY (user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_grammar_exercise_progress_exercise_fk
          FOREIGN KEY (exercise_id)
          REFERENCES community.vocab_grammar_exercise(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_grammar_exercise_progress_unique UNIQUE (user_id, exercise_id)
      );
    `);

    // Indizes
    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_grammar_exercise_lesson_idx
        ON community.vocab_grammar_exercise(lesson_id);
      CREATE INDEX IF NOT EXISTS vocab_grammar_exercise_type_idx
        ON community.vocab_grammar_exercise(exercise_type_id);
      CREATE INDEX IF NOT EXISTS vocab_grammar_exercise_progress_user_idx
        ON community.vocab_grammar_exercise_progress(user_id);
      CREATE INDEX IF NOT EXISTS vocab_grammar_exercise_progress_exercise_idx
        ON community.vocab_grammar_exercise_progress(exercise_id);
    `);

    // Standard-Übungstypen einfügen
    await queryInterface.sequelize.query(`
      INSERT INTO community.vocab_grammar_exercise_type (name, description) VALUES
        ('gap_fill', 'Lückentext-Übung'),
        ('multiple_choice', 'Multiple-Choice-Fragen'),
        ('sentence_building', 'Satzbau-Übung'),
        ('transformation', 'Satzumformung'),
        ('conjugation', 'Konjugations-Übung'),
        ('declension', 'Deklinations-Übung')
      ON CONFLICT (name) DO NOTHING;
    `);
  },

  async down(queryInterface) {
    await queryInterface.sequelize.query(`
      DROP TABLE IF EXISTS community.vocab_grammar_exercise_progress CASCADE;
      DROP TABLE IF EXISTS community.vocab_grammar_exercise CASCADE;
      DROP TABLE IF EXISTS community.vocab_grammar_exercise_type CASCADE;
    `);
  }
};
