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

module.exports = {
  async up(queryInterface) {
    // Kapitel innerhalb einer Sprache
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_chapter (
        id SERIAL PRIMARY KEY,
        language_id INTEGER NOT NULL,
        title TEXT NOT NULL,
        created_by_user_id INTEGER NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_chapter_language_fk
          FOREIGN KEY (language_id)
          REFERENCES community.vocab_language(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_chapter_creator_fk
          FOREIGN KEY (created_by_user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE
      );
    `);

    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_chapter_language_idx
      ON community.vocab_chapter(language_id);
    `);

    // Lexeme/Wörter (wir deduplizieren pro Sprache über normalized)
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_lexeme (
        id SERIAL PRIMARY KEY,
        language_id INTEGER NOT NULL,
        text TEXT NOT NULL,
        normalized TEXT NOT NULL,
        created_by_user_id INTEGER NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_lexeme_language_fk
          FOREIGN KEY (language_id)
          REFERENCES community.vocab_language(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_lexeme_creator_fk
          FOREIGN KEY (created_by_user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_lexeme_unique_per_language UNIQUE (language_id, normalized)
      );
    `);

    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_lexeme_language_idx
      ON community.vocab_lexeme(language_id);
    `);

    // n:m Zuordnung pro Kapitel: Lernwort ↔ Referenzwort (Mehrdeutigkeiten möglich)
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_chapter_lexeme (
        id SERIAL PRIMARY KEY,
        chapter_id INTEGER NOT NULL,
        learning_lexeme_id INTEGER NOT NULL,
        reference_lexeme_id INTEGER NOT NULL,
        created_by_user_id INTEGER NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_chlex_chapter_fk
          FOREIGN KEY (chapter_id)
          REFERENCES community.vocab_chapter(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_chlex_learning_fk
          FOREIGN KEY (learning_lexeme_id)
          REFERENCES community.vocab_lexeme(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_chlex_reference_fk
          FOREIGN KEY (reference_lexeme_id)
          REFERENCES community.vocab_lexeme(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_chlex_creator_fk
          FOREIGN KEY (created_by_user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_chlex_unique UNIQUE (chapter_id, learning_lexeme_id, reference_lexeme_id)
      );
    `);

    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_chlex_chapter_idx
      ON community.vocab_chapter_lexeme(chapter_id);
    `);
    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_chlex_learning_idx
      ON community.vocab_chapter_lexeme(learning_lexeme_id);
    `);
    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_chlex_reference_idx
      ON community.vocab_chapter_lexeme(reference_lexeme_id);
    `);
  },

  async down(queryInterface) {
    await queryInterface.sequelize.query(`DROP TABLE IF EXISTS community.vocab_chapter_lexeme;`);
    await queryInterface.sequelize.query(`DROP TABLE IF EXISTS community.vocab_lexeme;`);
    await queryInterface.sequelize.query(`DROP TABLE IF EXISTS community.vocab_chapter;`);
  }
};


