import mongoose, { Schema, Document } from "mongoose";

// 定义管理员用户的 TypeScript 接口
export interface IAdmin extends Document {
  username: string;
  password: string;
  isAdmin: boolean;
  createdAt: Date;
}

// 定义管理员用户的 Mongoose Schema
const AdminSchema: Schema = new Schema({
  // 用户名，确保唯一且移除两端空白字符
  username: {
    type: String,
    required: true,
    unique: true,
    trim: true,
    minlength: 3,
  },
  // 密码，需要最少6个字符
  password: {
    type: String,
    required: true,
    minlength: 6,
  },
  // 标识是否为管理员，默认值为true
  isAdmin: {
    type: Boolean,
    default: true,
  },
  // 记录创建时间
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

// 创建并导出 Admin 模型
const Admin = mongoose.model<IAdmin>("Admin", AdminSchema);

export default Admin;
