import { Schema, model, Document } from 'mongoose'

export interface IUser extends Document {
  ghostguildId: string // ID from ghostguild-org
  email: string
  username: string
  displayName: string
  avatar?: string
  roles: string[] // 'admin', 'moderator', 'member', 'cohort-X'
  permissions: {
    canEdit: boolean
    canModerate: boolean
    canAdmin: boolean
  }
  contributions: {
    edits: number
    comments: number
    articles: number
  }
  createdAt: Date
  updatedAt: Date
  lastLogin: Date
}

const userSchema = new Schema<IUser>({
  ghostguildId: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  username: { type: String, required: true, unique: true },
  displayName: { type: String, required: true },
  avatar: String,
  roles: [{ type: String }],
  permissions: {
    canEdit: { type: Boolean, default: false },
    canModerate: { type: Boolean, default: false },
    canAdmin: { type: Boolean, default: false }
  },
  contributions: {
    edits: { type: Number, default: 0 },
    comments: { type: Number, default: 0 },
    articles: { type: Number, default: 0 }
  },
  lastLogin: { type: Date, default: Date.now }
}, {
  timestamps: true
})

export const User = model<IUser>('User', userSchema)