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

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

  declare schoolId: string;

  declare semesterId: string;

  declare semesterName: string;

  declare examId: string;

  declare examName: string;

  declare courseId: string | null;

  declare courseName: string | null;

  declare courseCode: string | null;

  declare paperNumber: string | null;

  declare trackConfig: string;

  declare filterData: string;

  declare status: number;

  declare error: string | null;

  declare createdAt: CreationOptional<Date>;

  declare updatedAt: CreationOptional<Date>;
}

TrackTask.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    schoolId: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    semesterId: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    semesterName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    examId: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    examName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    courseId: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    courseName: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    courseCode: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    paperNumber: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    trackConfig: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '导出设置',
    },
    filterData: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '详情条件',
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: TRACK_TASK_STATUS.INIT,
    },
    error: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: '错误信息',
    },
    createdAt: DataTypes.DATE,
    updatedAt: DataTypes.DATE,
  },
  {
    sequelize,
    modelName: 'TrackTask',
    underscored: true,
    tableName: 'trackTask',
  }
);

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

export type TrackTaskData = InferAttributes<TrackTask>;

export default TrackTask;
