'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('vision_cache', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      device_udid: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      instruction: {
        type: Sequelize.TEXT,
        allowNull: false,
      },
      action: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      value: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      element_name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      min_x: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      min_y: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      max_x: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      max_y: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      center_x: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      center_y: {
        type: Sequelize.FLOAT,
        allowNull: false,
        defaultValue: 0,
      },
      container_width: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      container_height: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        allowNull: false,
      },
    });

    await queryInterface.addIndex(
      'vision_cache',
      ['device_udid', 'instruction', 'action', 'element_name'],
      {
        name: 'vision_cache_lookup_idx',
      }
    );
  },

  async down(queryInterface) {
    await queryInterface.dropTable('vision_cache');
  },
};
