func PhoneExists(mobile string, ms *services.MySQL) bool {
	query := "SELECT id FROM users WHERE mobile=?"
	res, _ := ms.Query(query, mobile)

	if res.Next() {
		var userId int
		res.Scan(&userId)
		if userId > 0 {
			return true
		}
	}
	return false
}
func DeleteBloodRequest(r *BloodRequest, ms *services.MySQL) (bool, *app.Msg, error) {

	status, error := r.ValidateDeleteReq()
	if status == false {
		return false, app.NewErrMsg(error), nil
	}
	query := "DELETE FROM requests WHERE id = ,?)"
	_, dbError := ms.Exec(query, r.ReqId)
	if dbError != nil {
		return false, nil, dbError
	}
	return false, app.NewErrMsg(error), nil
}
func AuthenticateUser(mobile, password *string, ms *services.MySQL) (*User, *app.Msg) {
	u := User{}
	query := "SELECT id, password  FROM users WHERE mobile = ?"
	dbError := ms.QueryRow(query, mobile).Scan(&u.Id, &u.Password)
	if dbError != nil {
		return nil, app.NewErrMsg("Invalid credentials")
	} else {
		err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(*password))
		if err != nil {
			return nil, app.NewErrMsg("Invalid credentials")
		}

		return &u, nil
	}
}
func CreateBloodRequest(r *BloodRequest, ms *services.MySQL) (bool, *app.Msg, error) {

	status, error := r.ValidateBloodReq()
	if status == false {
		return false, app.NewErrMsg(error), nil
	}
	query := "INSERT INTO requests(user_id, date_of_requirement, location, place_id, blood, comments, mobile) VALUES(?,?,POINT(" + r.Lat + ", " + r.Lng + "),?,?,?,?)"
	const createdFormat = "2006-01-02 15:04:05" //"Jan 2, 2006 at 3:04pm (MST)"

	utcTime, _ := strconv.ParseInt(r.Date, 10, 64)
	utcTime1 := time.Unix(utcTime, 0).Format(createdFormat)
	_, dbError := ms.Exec(query, r.UserId, utcTime1, r.PlaceId, r.Blood, r.Description, r.Phone)
	if dbError != nil {
		return false, nil, dbError
	}
	return false, app.NewErrMsg(error), nil
}
func RegisterUser(u *User, ms *services.MySQL) (int64, *app.Msg, error) {
	status, error := ValidateUser(u, ms)
	if status == false {
		return 0, app.NewErrMsg(error), nil
	}

	passwordHash, _ := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
	query := "INSERT INTO users(name, mobile, password, blood, sex, location, place_id) VALUES(?,?,?,?,?, POINT(" + u.Lat + ", " + u.Lng + "),?)"
	res, dbError := ms.Exec(query, u.Name, u.Mobile, string(passwordHash[:]), u.Blood, u.Sex, u.PlaceId)
	if dbError != nil {
		return 0, nil, dbError
	} else {
		id, err := res.LastInsertId()
		if err != nil {
			return 0, nil, err
		} else {
			return id, nil, nil
		}
	}

	return 0, app.NewErrMsg(error), nil
}
func GetBloodRequest(r *BloodRequest, ms *services.MySQL) (bool, *BloodRequest, error) {

	// status, _ := r.ValidateGetReq()
	// if status == false {
	// 	return nil, nil //TODO return validation err
	// }
	query := "SELECT * FROM requests WHERE id = , ?)"
	dbRows, dbError := ms.Query(query, r.ReqId)
	if dbError == nil {
		return false, r, dbError
	}
	defer dbRows.Close()

	if dbRows.Next() {
		var (
			result      BloodRequest
			reqId       string
			userId      string
			date        string
			blood       string
			description string
			placeId     string
		)
		dbRows.Scan(&reqId, &userId, &date, &blood, &description, &placeId)
		result = BloodRequest{
			ReqId:       reqId,
			UserId:      userId,
			Date:        date,
			Blood:       blood,
			Description: description,
			PlaceId:     placeId,
		}
		// ReqId UserId Date Blood Phone Description Lat Lng PlaceId
		// dbRows.Scan(r.ReqId, r.UserId, r.Date, r.Blood, r.Description, r.PlaceId)
		return true, &result, nil
	}
	return false, r, nil
}