/**
 * 电子围栏坐标点数据表迁移
 * 用于存储围栏绘制过程中用户选定的经纬度坐标点
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建电子围栏坐标点表
    await queryInterface.createTable('electronic_fence_points', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '主键ID'
      },
      fence_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '关联的围栏ID',
        references: {
          model: 'electronic_fences',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      point_order: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '坐标点在围栏中的顺序（从0开始）'
      },
      longitude: {
        type: Sequelize.DECIMAL(10, 7),
        allowNull: false,
        comment: '经度'
      },
      latitude: {
        type: Sequelize.DECIMAL(10, 7),
        allowNull: false,
        comment: '纬度'
      },
      point_type: {
        type: Sequelize.ENUM('corner', 'control', 'marker'),
        allowNull: false,
        defaultValue: 'corner',
        comment: '坐标点类型：corner-拐角点，control-控制点，marker-标记点'
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '坐标点描述信息'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '是否激活'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '创建人ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '更新人ID',
        references: {
          model: 'users',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    });

    // 添加索引
    await queryInterface.addIndex('electronic_fence_points', ['fence_id'], {
      name: 'idx_fence_points_fence_id'
    });

    await queryInterface.addIndex('electronic_fence_points', ['fence_id', 'point_order'], {
      name: 'idx_fence_points_fence_order'
    });

    await queryInterface.addIndex('electronic_fence_points', ['longitude', 'latitude'], {
      name: 'idx_fence_points_coordinates'
    });

    await queryInterface.addIndex('electronic_fence_points', ['point_type'], {
      name: 'idx_fence_points_type'
    });

    await queryInterface.addIndex('electronic_fence_points', ['is_active'], {
      name: 'idx_fence_points_active'
    });

    console.log('✅ 电子围栏坐标点表创建成功');
  },

  down: async (queryInterface, Sequelize) => {
    // 删除表
    await queryInterface.dropTable('electronic_fence_points');
    console.log('✅ 电子围栏坐标点表删除成功');
  }
};
