import { DataTypes } from 'sequelize';
import sequelize from '../database.js';

const MemberTtrHistory = sequelize.define('MemberTtrHistory', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  memberId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'member_id'
  },
  clubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'club_id'
  },
  playerId: {
    type: DataTypes.STRING,
    allowNull: true,
    field: 'player_id'
  },
  sourceDate: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    field: 'source_date'
  },
  ttr: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  qttr: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  label: {
    type: DataTypes.STRING,
    allowNull: true
  },
  sourceType: {
    type: DataTypes.STRING,
    allowNull: true,
    field: 'source_type'
  },
  fetchedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    field: 'fetched_at'
  },
  rawPayload: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'raw_payload'
  }
}, {
  underscored: true,
  tableName: 'member_ttr_history',
  timestamps: true,
  indexes: [
    {
      fields: ['member_id']
    },
    {
      fields: ['club_id']
    },
    {
      fields: ['player_id']
    },
    {
      fields: ['source_date']
    }
  ]
});

export default MemberTtrHistory;
