class AuthAPI {
    async validate_username(username) {
        const result = await axios.post('/api/v1/auth/validate/username', { username })
        return result && result.data && result.data.data && result.data.data.is_valid
    }

    async validate_email(email) {
        const result = await axios.post('/api/v1/auth/validate/email', { email })
        return result && result.data && result.data.data && result.data.data.is_valid
    }

    async username_taken(username) {
        const result = await axios.post('/api/v1/auth/validate/user_exists', { username })
        return result && result.data && result.data.data && result.data.data.username_taken
    }

    async email_taken(email) {
        const result = await axios.post('/api/v1/auth/validate/user_exists', { email })
        return result && result.data && result.data.data && result.data.data.email_taken
    }

    async attempt({ username, password, create_session, ...others }) {
        try {
            const result = await axios.post('/api/v1/auth/attempt', {
                username, password, create_session, ...others
            })

            if ( result && result.data && result.data.data && result.data.data ) {
                return result.data.data
            }
        } catch (e) {}

        return { success: false }
    }

    async mfa_generate() {
        const result = await axios.post('/api/v1/auth/mfa/generate')
        return result && result.data && result.data.data
    }

    async mfa_attempt(verify_code) {
        const result = await axios.post('/api/v1/auth/mfa/attempt', { verify_code })
        return result && result.data && result.data.data
    }

    async mfa_enable() {
        const result = await axios.post('/api/v1/auth/mfa/enable')
        return result && result.data && result.data.data && result.data.data.success && result.data.data.mfa_enabled
    }

    async mfa_disable() {
        const result = await axios.post('/api/v1/auth/mfa/disable')
        return result && result.data && result.data.data && result.data.data.success && !result.data.data.mfa_enabled
    }

    async has_mfa() {
        const result = await axios.get('/api/v1/auth/mfa/enable/date')
        if ( result && result.data && result.data.data ) return result.data.data
    }

    async has_mfa_recovery() {
        const result = await axios.get('/api/v1/auth/mfa/recovery')
        if ( result && result.data && result.data.data ) return result.data.data
    }

    async generate_mfa_recovery() {
        const result = await axios.post('/api/v1/auth/mfa/recovery')
        if ( result && result.data && result.data.data && result.data.data.codes ) return result.data.data.codes
    }

    async attempt_mfa_recovery(code) {
        const result = await axios.post('/api/v1/auth/mfa/recovery/attempt', { code })
        if ( result && result.data && result.data.data ) return result.data.data
    }

    async app_passwords() {
        const result = await axios.get('/api/v1/password/app_passwords')
        if ( result && result.data && Array.isArray(result.data.data) ) return result.data.data
    }

    async create_app_password(name) {
        const result = await axios.post('/api/v1/password/app_passwords', { name })
        if ( result && result.data && result.data.data ) return result.data.data
    }

    async get_authenticated_user() {
        const result = await axios.get('/api/v1/auth/authenticated_user')
        if ( result && result.data && result.data.data && result.data.data.authenticated )
            return result.data.data.uid
    }

    async delete_app_password(uuid) {
        await axios.delete(`/api/v1/password/app_passwords/${uuid}`)
    }

    async register_user({ first_name, last_name, uid, email }) {
        const result = await axios.post('/api/v1/auth/registration', { first_name, last_name, uid, email })
        if ( result && result.data && result.data.data ) return result.data.data
    }
}

const auth_api = new AuthAPI()
export { auth_api }
