package models

import (
	"net/http"

	"code.gitea.io/gitea/modules/timeutil"
)

type UserLoginLog struct {
	ID          int64              `xorm:"pk autoincr"`
	UId         int64              `xorm:"NOT NULL"`
	IpAddr      string             `xorm:"default NULL"`
	CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

func SaveLoginInfoToDb(r *http.Request, u *User) {
	statictisSess := xStatistic.NewSession()
	defer statictisSess.Close()

	var dateRecord UserLoginLog

	dateRecord.UId = u.ID
	dateRecord.IpAddr = getIP(r)

	statictisSess.Insert(&dateRecord)
}

func getIP(r *http.Request) string {
	forwarded := r.Header.Get("X-FORWARDED-FOR")
	if forwarded != "" {
		return forwarded
	}
	return r.RemoteAddr
}
