package services_astrologer

import (
	"astrology-api/constants"
	models "astrology-api/models/astrologermodel"
)

// Screen names the astrologer app switches on. They are returned by login,
// verify-login, the OTP verify calls and the profile progress call, and all four
// derive them from the same place so no two disagree about where an astrologer
// should land.
const (
	// ScreenVerifyMobile / ScreenVerifyEmail: registration OTPs still pending.
	ScreenVerifyMobile = "verify_mobile"

	ScreenVerifyEmail = "verify_email"

	// ScreenVerifyLoginOTP: login OTP sent, waiting for /verify-login.
	ScreenVerifyLoginOTP = "verify_login_otp"

	// ScreenCreateProfile: the 1..10 profile flow, resumed at pending_step.
	ScreenCreateProfile = "create_profile"

	// ScreenProfileUnderReview: submitted, waiting on admin or interview.
	ScreenProfileUnderReview = "profile_under_review"

	// ScreenDashboard: approved.
	ScreenDashboard = "dashboard"
)

// ProfileStepTitles is the single list of the ten profile steps.
var ProfileStepTitles = map[int]string{
	constants.StepBasicProfile: "Basic Information",
	constants.StepLanguages:    "Languages",
	constants.StepSkills:       "Skills",
	constants.StepProfileImage: "Profile Image",
	constants.StepEducation:    "Education",
	constants.StepProfessional: "Professional",
	constants.StepExperience:   "Experience",
	constants.StepBank:         "Bank Details",
	constants.StepDocuments:    "Documents",
	constants.StepAvailability: "Work Availability",
}

// ProfileStepTitle names one step, or "" when the number is out of range.
func ProfileStepTitle(step int) string {

	return ProfileStepTitles[step]
}

// PendingProfileStep is the step the app must open next.
//
// updateProgress only ever raises CurrentStep to the step just saved, so the
// pending step is the one after it. A profile with no astrologer row yet has not
// saved anything, and one that has saved all ten stays on the last step, where
// the app submits.
func PendingProfileStep(astrologer *models.Astrologer) int {

	if astrologer == nil || astrologer.CurrentStep <= 0 {
		return constants.StepBasicProfile
	}

	if astrologer.CurrentStep >= constants.StepAvailability {
		return constants.StepAvailability
	}

	return astrologer.CurrentStep + 1
}

// AllProfileStepsDone reports whether every one of the ten steps has been saved,
// which is the point where /profile/submit becomes available.
func AllProfileStepsDone(astrologer *models.Astrologer) bool {

	return astrologer != nil && astrologer.CurrentStep >= constants.StepAvailability
}

// ProfileScreen decides where an astrologer belongs from the row itself.
//
// This is the routing plans.docx describes: an incomplete profile resumes at its
// pending step, a submitted one waits under review, an approved one gets the
// dashboard. A nil astrologer is somebody who verified their OTPs and never
// saved step 1 — the profile flow from the top, not an error.
func ProfileScreen(astrologer *models.Astrologer) string {

	if astrologer == nil {
		return ScreenCreateProfile
	}

	switch astrologer.ProfileStatus {

	case constants.ProfileApproved:
		return ScreenDashboard

	case constants.ProfileSubmitted,
		constants.ProfileUnderReview,
		constants.ProfileInterviewRound1,
		constants.ProfileInterviewRound2,
		constants.ProfileDocumentsVerified:

		return ScreenProfileUnderReview
	}

	// INCOMPLETE, IN_PROGRESS, NOTSUBMITTED, REJECTED and an empty status are all
	// editable, so they go back into the step flow. profile_status travels with
	// the response, which is how the app tells a rejection from a fresh start.
	return ScreenCreateProfile
}

// RegistrationScreen is the screen for an account whose registration OTPs are
// not both verified yet. It returns "" once verification is complete, so the
// caller can fall through to ProfileScreen.
func RegistrationScreen(user *models.User) string {

	if user == nil {
		return ""
	}

	if !user.MobileVerified {
		return ScreenVerifyMobile
	}

	if !user.EmailVerified {
		return ScreenVerifyEmail
	}

	return ""
}
