import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";

export type UserCreationAttributes = InferCreationAttributes<UserModel>;

export class UserModel extends Model<
  InferAttributes<UserModel>,
  InferCreationAttributes<UserModel>
> {
  // To avoid table creation
  /*static async sync(): Promise<any> {
      return Promise.resolve();
    }*/

  declare id: string;
  declare username: string;
  declare email: string;
}

export default (sequelize: Sequelize) => {
  UserModel.init(
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
      },
      username: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      sequelize,
      tableName: "users",
      paranoid: true, // softs deletes
      timestamps: true,

      createdAt: "created_at",
      updatedAt: "updated_at",
      deletedAt: "deleted_at",

      indexes: [{ name: "email_idx", fields: ["email"], unique: true }],

      whereMergeStrategy: "and", // <- cómo tratar el merge de un scope

      defaultScope: {},

      scopes: {},
    }
  );
  return UserModel;
};
