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

const UserLevelProgress = sequelize.define('UserLevelProgress', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userProgressId: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  levelId: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  score: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  moves: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  time: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  stars: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  isCompleted: {
    type: DataTypes.BOOLEAN,
    defaultValue: false
  },
  attempts: {
    type: DataTypes.INTEGER,
    defaultValue: 1
  },
  bestScore: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  bestMoves: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  bestTime: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  completedAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'match3_user_level_progress',
  schema: 'match3',
  timestamps: true,
  underscored: true,  // WICHTIG: Alle Datenbankfelder im snake_case Format
  indexes: [
    {
      unique: true,
      fields: ['user_progress_id', 'level_id']  // WICHTIG: Bei underscored: true müssen snake_case Namen verwendet werden
    }
  ]
});

export default UserLevelProgress;
