package dto_astrologer

import "strings"

// DeviceSession is the handset context /verify-email and /verify-login accept
// alongside the OTP. Every field is optional: an app that sends none of them
// still verifies, it just leaves no login_histories row behind.
//
// Platform is free text — apps send values like
// "Android-location-nimbahera dist chittorgarh rajasthan 312601" — so it is
// stored as given in login_histories and only mapped onto users.device_type
// when it starts with a value that enum knows.
type DeviceSession struct {
	IPAddress   string `json:"ip_address"`
	DeviceName  string `json:"device_name"`
	Platform    string `json:"platform"`
	DeviceToken string `json:"device_token"`
	DeviceType  string `json:"device_type"`
}

// HasSession reports whether there is anything worth writing to login_histories.
func (d DeviceSession) HasSession() bool {

	return strings.TrimSpace(d.IPAddress) != "" ||
		strings.TrimSpace(d.DeviceName) != "" ||
		strings.TrimSpace(d.Platform) != ""
}

// ResolvedDeviceType is what to store in users.device_type (ANDROID / IPHONE /
// WEB), or "" to leave the column alone. An explicit device_type wins;
// otherwise the platform's leading word is used.
func (d DeviceSession) ResolvedDeviceType() string {

	if value := strings.TrimSpace(d.DeviceType); value != "" {
		return strings.ToUpper(value)
	}

	platform := strings.ToLower(strings.TrimSpace(d.Platform))

	switch {
	case strings.HasPrefix(platform, "android"):
		return "ANDROID"
	case strings.HasPrefix(platform, "ios"), strings.HasPrefix(platform, "iphone"):
		return "IPHONE"
	case strings.HasPrefix(platform, "web"):
		return "WEB"
	}

	return ""
}
