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

module.exports = {
  async up(queryInterface) {
    // Sprache / Set, das geteilt werden kann
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_language (
        id SERIAL PRIMARY KEY,
        owner_user_id INTEGER NOT NULL,
        name TEXT NOT NULL,
        share_code TEXT NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_language_owner_fk
          FOREIGN KEY (owner_user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_language_share_code_uniq UNIQUE (share_code)
      );
    `);

    // Abos (Freunde)
    await queryInterface.sequelize.query(`
      CREATE TABLE IF NOT EXISTS community.vocab_language_subscription (
        id SERIAL PRIMARY KEY,
        user_id INTEGER NOT NULL,
        language_id INTEGER NOT NULL,
        created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
        CONSTRAINT vocab_language_subscription_user_fk
          FOREIGN KEY (user_id)
          REFERENCES community."user"(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_language_subscription_language_fk
          FOREIGN KEY (language_id)
          REFERENCES community.vocab_language(id)
          ON DELETE CASCADE,
        CONSTRAINT vocab_language_subscription_uniq UNIQUE (user_id, language_id)
      );
    `);

    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_language_owner_idx
      ON community.vocab_language(owner_user_id);
    `);
    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_language_subscription_user_idx
      ON community.vocab_language_subscription(user_id);
    `);
    await queryInterface.sequelize.query(`
      CREATE INDEX IF NOT EXISTS vocab_language_subscription_language_idx
      ON community.vocab_language_subscription(language_id);
    `);
  },

  async down(queryInterface) {
    await queryInterface.sequelize.query(`DROP TABLE IF EXISTS community.vocab_language_subscription;`);
    await queryInterface.sequelize.query(`DROP TABLE IF EXISTS community.vocab_language;`);
  }
};


