import {
  Model,
  DataTypes,
  CreationOptional,
  InferAttributes,
  InferCreationAttributes,
} from 'sequelize';
import sequelize from '../sequelizeInstance';
import TrackTask from './trackTask';
import { TRACK_TASK_DETAIL_STATUS } from '../enumerate';

class TrackTaskDetail extends Model<
  // eslint-disable-next-line no-use-before-define
  InferAttributes<TrackTaskDetail>,
  // eslint-disable-next-line no-use-before-define
  InferCreationAttributes<TrackTaskDetail>
> {
  declare id: CreationOptional<number>;

  declare trackTaskId: number;

  declare studentId: string;

  declare studentName: string;

  declare studentCode: string;

  declare className: string;

  declare status: number;

  declare error: string | null;

  declare createdAt: CreationOptional<Date>;

  declare updatedAt: CreationOptional<Date>;
}

TrackTaskDetail.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    trackTaskId: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    studentId: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    studentName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    studentCode: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    className: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: TRACK_TASK_DETAIL_STATUS.INIT,
    },
    error: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: '错误信息',
    },
    createdAt: DataTypes.DATE,
    updatedAt: DataTypes.DATE,
  },
  {
    sequelize,
    modelName: 'TrackTaskDetail',
    underscored: true,
    tableName: 'trackTaskDetail',
  }
);

TrackTask.hasMany(TrackTaskDetail);
TrackTaskDetail.belongsTo(TrackTask);

export type TrackTaskDetailCreationAttributes = InferCreationAttributes<
  TrackTaskDetail,
  { omit: 'id' | 'createdAt' | 'updatedAt' | 'error' }
>;

export type TrackTaskDetailData = InferAttributes<TrackTaskDetail>;

export default TrackTaskDetail;
