Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.enumuration.dtr;

/**
 * LMS lead lifecycle stage (SOP §8). This is the richer, forward-facing lifecycle the
 * LMS screens read off. It sits ALONGSIDE the legacy {@link LeadStatus} (5 values) — every
 * mutation writes {@code toLegacyStatus()} back onto {@code Lead.status} so the existing
 * lists / exports / charts / geo / beat flows keep working untouched.
 *
 * NEW → ASSIGNED → CONTACTED → QUALIFIED → BEAT_PLANNED → VISITED → ONBOARDED → ACTIVE,
 * with two terminal off-path states: NOT_INTERESTED and DROPPED.
 */
public enum LeadStage {
    NEW,
    ASSIGNED,
    CONTACTED,
    QUALIFIED,
    BEAT_PLANNED,
    VISITED,
    ONBOARDED,
    ACTIVE,
    NOT_INTERESTED,
    DROPPED;

    /** Ordered happy-path (excludes terminal off-path states) — drives the stepper + forward-transition checks. */
    public static final LeadStage[] HAPPY_PATH = {
            NEW, ASSIGNED, CONTACTED, QUALIFIED, BEAT_PLANNED, VISITED, ONBOARDED, ACTIVE
    };

    /**
     * Map a stage onto the legacy {@link LeadStatus} so existing queries stay correct.
     * pending = open/in-progress, followUp = qualified/scheduled, finalized = won, notInterested = lost/dead.
     */
    public LeadStatus toLegacyStatus() {
        switch (this) {
            case NEW:
            case ASSIGNED:
            case CONTACTED:
                return LeadStatus.pending;
            case QUALIFIED:
            case BEAT_PLANNED:
            case VISITED:
                return LeadStatus.followUp;
            case ONBOARDED:
            case ACTIVE:
                return LeadStatus.finalized;
            case NOT_INTERESTED:
            case DROPPED:
            default:
                return LeadStatus.notInterested;
        }
    }

    /**
     * Best-effort stage for a pre-existing lead that has no {@code stage} column value yet
     * (rows created before the LMS columns were added). Used on read only.
     */
    public static LeadStage fromLegacy(LeadStatus status, String color) {
        if (status == null) {
            return NEW;
        }
        switch (status) {
            case pending:
                return ASSIGNED;   // already created + assigned, first contact not yet logged
            case followUp:
                return QUALIFIED;  // in the working pipeline
            case finalized:
                return ONBOARDED;
            case notInterested:
                return NOT_INTERESTED;
            default:
                return NEW;
        }
    }

    /** Is {@code next} a forward move from this stage along the happy path? Terminal states are always allowed. */
    public boolean canAdvanceTo(LeadStage next) {
        if (next == NOT_INTERESTED || next == DROPPED) {
            return true;
        }
        int here = indexOf(this);
        int there = indexOf(next);
        return here >= 0 && there > here;
    }

    private static int indexOf(LeadStage s) {
        for (int i = 0; i < HAPPY_PATH.length; i++) {
            if (HAPPY_PATH[i] == s) {
                return i;
            }
        }
        return -1;
    }
}