/**
 * 电子围栏表迁移
 * 创建电子围栏相关表结构
 */

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建电子围栏表
    await queryInterface.createTable('electronic_fences', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '围栏ID'
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '围栏名称'
      },
      type: {
        type: Sequelize.ENUM('collector', 'grazing', 'safety'),
        allowNull: false,
        defaultValue: 'collector',
        comment: '围栏类型: collector-采集器电子围栏, grazing-放牧围栏, safety-安全围栏'
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '围栏描述'
      },
      coordinates: {
        type: Sequelize.JSON,
        allowNull: false,
        comment: '围栏坐标点数组'
      },
      center_lng: {
        type: Sequelize.DECIMAL(10, 7),
        allowNull: false,
        comment: '围栏中心经度'
      },
      center_lat: {
        type: Sequelize.DECIMAL(10, 7),
        allowNull: false,
        comment: '围栏中心纬度'
      },
      area: {
        type: Sequelize.DECIMAL(10, 4),
        allowNull: true,
        comment: '围栏面积(平方米)'
      },
      grazing_status: {
        type: Sequelize.ENUM('grazing', 'not_grazing'),
        allowNull: false,
        defaultValue: 'not_grazing',
        comment: '放牧状态: grazing-放牧中, not_grazing-未放牧'
      },
      inside_count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '安全区域内动物数量'
      },
      outside_count: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '安全区域外动物数量'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '是否启用'
      },
      farm_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '关联农场ID',
        references: {
          model: 'farms',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      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: '更新时间'
      }
    }, {
      comment: '电子围栏表',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci'
    });

    // 创建索引
    await queryInterface.addIndex('electronic_fences', ['name'], {
      name: 'idx_fence_name'
    });

    await queryInterface.addIndex('electronic_fences', ['type'], {
      name: 'idx_fence_type'
    });

    await queryInterface.addIndex('electronic_fences', ['center_lng', 'center_lat'], {
      name: 'idx_fence_center'
    });

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

    await queryInterface.addIndex('electronic_fences', ['farm_id'], {
      name: 'idx_fence_farm_id'
    });

    await queryInterface.addIndex('electronic_fences', ['grazing_status'], {
      name: 'idx_fence_grazing_status'
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 删除表
    await queryInterface.dropTable('electronic_fences');
  }
};
