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

const User = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: true,
    validate: {
      isEmail: true,
    },
  },
  nickname: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  provider: {
    type: DataTypes.ENUM('google', 'naver', 'kakao'),
    allowNull: false,
    comment: 'Social login provider',
  },
  socialId: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: 'Unique ID from the social provider',
  },
  refreshToken: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: 'Generic refresh token for valid session',
  },
}, {
  indexes: [
    {
      unique: true,
      fields: ['provider', 'socialId'], // Prevent duplicate users for same provider
    },
  ],
  timestamps: true,
});

module.exports = User;
