const { DataTypes } = require("sequelize");
const sequelize = require("../config/db");

const Player = sequelize.define("Player", {
  id: {
    type: DataTypes.STRING,
    primaryKey: true,
  },
  username: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
  },
  experience: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
  },
  credits: {
    type: DataTypes.INTEGER,
    defaultValue: 500,
  },
  lastCreditUpdate: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  energy: {
    type: DataTypes.INTEGER,
    defaultValue: 100,
  },
  maxEnergy: {
    type: DataTypes.INTEGER,
    defaultValue: 100,
  },
  lastEnergyRegen: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  lastLogin: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  equipment: {
    type: DataTypes.TEXT,
    defaultValue: "{}",
    get() {
      const val = this.getDataValue("equipment");
      return val ? JSON.parse(val) : {};
    },
    set(val) {
      this.setDataValue("equipment", JSON.stringify(val));
    },
  },
});

module.exports = Player;
