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

const Login = sequelize.define('Login', {
  login_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  treelogy_id: { type: DataTypes.INTEGER, allowNull: true }, // Allow null for Super Admin
  security_no: { type: DataTypes.STRING(100), allowNull: false, defaultValue: '0' },
  com_id: { type: DataTypes.INTEGER, allowNull: true }, // Allow null for Super Admin
  client_id: { type: DataTypes.INTEGER, allowNull: true }, // Allow null for Super Admin
  current_mapping: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 },
  member_id: { type: DataTypes.STRING(100), allowNull: false, unique: true }, // Made UNIQUE
  mobile_number: { type: DataTypes.STRING(100), allowNull: false, defaultValue: '0' },
  is_mobile_number_verified: { type: DataTypes.BIGINT, allowNull: false, defaultValue: 0 },
  password: { type: DataTypes.STRING(100), allowNull: false },
  role_id: { 
    type: DataTypes.INTEGER, 
    allowNull: false,
    references: {
      model: 'user_master',
      key: 'id'
    }
  },
  name: { type: DataTypes.STRING(100), allowNull: false },
  updated_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
  status: { type: DataTypes.STRING(100), allowNull: false, defaultValue: 'Y' },
}, {
  tableName: 'login',
  schema: 'public',
  timestamps: false,
  indexes: [
    // removed invalid idx_society_id
    { name: 'idx_treelogy_id', fields: ['treelogy_id'] },
    { name: 'idx_member_id_unique', fields: ['member_id'], unique: true }
  ]
});

export default Login;
