from django.apps import AppConfig


class PipelineConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "apps.pipeline"

    def ready(self):
        from django.db.models.signals import post_migrate
        post_migrate.connect(seed_stages, sender=self)


# code, name, outcome  — refined recruitment pipeline
DEFAULT_STAGES = [
    ("contacted", "Contacted", "IN_PROGRESS"),
    ("candidate_responded", "Candidate Responded", "IN_PROGRESS"),
    ("qualifying", "Qualifying", "IN_PROGRESS"),
    ("submitted", "Submitted", "IN_PROGRESS"),
    ("interviewing", "Interviewing", "IN_PROGRESS"),
    ("selected", "Selected", "IN_PROGRESS"),
    ("offered", "Offered", "IN_PROGRESS"),
    ("placed", "Placed / Joined", "WON"),
    ("not_in_consideration", "Not in Consideration", "LOST"),
    ("offer_declined", "Offer Declined", "LOST"),
    ("offered_not_joined", "Offered but Not Joined", "LOST"),
    ("client_declined", "Client Declined", "LOST"),
]


def seed_stages(sender, **kwargs):
    """Seed default pipeline stages once (only when the table is empty)."""
    from .models import PipelineStage
    if PipelineStage.objects.exists():
        return
    for i, (code, name, outcome) in enumerate(DEFAULT_STAGES, start=1):
        PipelineStage.objects.create(code=code, name=name, outcome=outcome, sort_order=i, is_active=True)
