import { DateTime } from 'luxon'
import { beforeSave, column } from '@ioc:Adonis/Lucid/Orm'
import Hash from '@ioc:Adonis/Core/Hash'
import AppBaseModel from './AppBaseModel'

export enum UserRoles {
    Admin = 'admin',
    Operator = 'operator',
    Api = 'api',
    User = 'user'
}

export default class User extends AppBaseModel {
    @beforeSave()
    public static async hashPassword(user: User) {
        if (user.$dirty.password) {
            user.password = await Hash.make(user.password!)
        }
    }

    @column({ isPrimary: true })
    public id: number

    @column.dateTime({ autoCreate: true })
    public createdAt: DateTime

    @column.dateTime({ autoCreate: true, autoUpdate: true })
    public updatedAt: DateTime

    @column()
    public role: UserRoles = UserRoles.User

    @column()
    public username: string

    @column()
    public phone?: string

    @column()
    public email?: string

    @column({ serializeAs: null })
    public password?: string

    @column()
    public avatar?: string

    @column()
    public referrer?: number

    @column()
    public visitor: boolean
}
