const Sequelize = require("sequelize");

const sequelize = new Sequelize(
  "mysql://alex:alex88003@crossmap.co.kr:3306/CROSSMAP_APPS?charset=utf8mb4",
  {
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000,
      charset: 'utf8mb4'
    },
    timezone: "+09:00"
  }
);

sequelize
  .authenticate()
  .then(() => {
    console.log("Connection has been established successfully.");
  })
  .catch(err => {
    console.error("Unable to connect to the database:", err);
  });

class AppType extends Sequelize.Model { }
AppType.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: Sequelize.STRING,
    description: Sequelize.STRING,
    appCount: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE
  },
  { sequelize, modelName: "app_type", freezeTableName: true, timestamps: false }
);

class App extends Sequelize.Model { }
App.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: Sequelize.STRING,
    description: Sequelize.STRING,
    appType: Sequelize.INTEGER,
    status: Sequelize.STRING,
    developer: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE
  },
  { sequelize, modelName: "app", freezeTableName: true, timestamps: false }
);

class Category extends Sequelize.Model { }
Category.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    appType: Sequelize.INTEGER,
    app: Sequelize.INTEGER,
    type: Sequelize.STRING,
    name: Sequelize.STRING,
    parent: Sequelize.INTEGER,
    image: Sequelize.STRING,
    subCount: Sequelize.INTEGER,
    videoCount: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE
  },
  { sequelize, modelName: "category", freezeTableName: true, timestamps: false }
);

class Developer extends Sequelize.Model { }
Developer.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: Sequelize.STRING,
    description: Sequelize.STRING,
    appCount: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE
  },
  {
    sequelize,
    modelName: "developer",
    freezeTableName: true,
    timestamps: false
  }
);

class Video extends Sequelize.Model { }
Video.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    code: Sequelize.STRING,
    title: Sequelize.STRING(191),
    duration: Sequelize.TIME,
    description: Sequelize.STRING,
    album: Sequelize.STRING,
    artist: Sequelize.STRING,
    viewCount: Sequelize.INTEGER,
    likeCount: Sequelize.INTEGER,
    commentCount: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE,
  },
  { sequelize, modelName: "video", freezeTableName: true, timestamps: false }
);

class VideoCategory extends Sequelize.Model { }
VideoCategory.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    code: Sequelize.STRING,
    app: Sequelize.INTEGER,
    category: Sequelize.INTEGER,
    dateCreated: Sequelize.DATE,
    dateUpdated: Sequelize.DATE,
    dateDeleted: Sequelize.DATE,
  },
  {
    sequelize, modelName: "video_category", freezeTableName: true, timestamps: false,
    indexes: [
      {
        unique: true,
        fields: ['id', 'code', 'category']
      }
    ]
  }
);

VideoCategory.hasOne(Video, { as: 'v', foreignKey: 'code', sourceKey: 'code' });
VideoCategory.hasOne(Category, { as: 'c', foreignKey: 'id', sourceKey: 'category' });
VideoCategory.hasOne(App, { as: 'a', foreignKey: 'id', sourceKey: 'app' });

module.exports = {
  Sequelize,
  App,
  AppType,
  Category,
  Video,
  Developer,
  VideoCategory
};
