# stage/unified_state.py
import copy
from enum import Enum


class ConversationPhase(str, Enum):
    PRESENTATION = "presentation"
    ARGUMENTATION = "argumentation"
    DATA_CONFIRMATION = "data_confirmation"
    FORMALIZATION = "formalization"
    DONE = "done"


class UnifiedState:
    """Estado unificado que consolida os 4 estágios da conversa."""

    def __init__(
        self,
        expected_cpf_last_3: str = "",
        expected_birth_date: str = "",
    ):
        # ── Global ──
        self.current_phase = ConversationPhase.PRESENTATION
        self.end_conversation = False

        # ── Presentation ──
        self.name_validation_attempt = 0
        self.is_target_customer = False

        # ── Argumentation ──
        self.arg_declines = 0
        self.arg_accepted_count = 0
        self.arg_accepted = False
        self.arg_off_topic = 0

        # ── Data Confirmation ──
        self.expected_cpf_last_3_digits = expected_cpf_last_3
        self.expected_birth_date_YYYY_MM_DD = expected_birth_date
        self.cpf_last_3_digits = None
        self.birth_date_YYYY_MM_DD = None
        self.validated_target_customer = False
        self.validated_cpf = False
        self.validated_birth_date = False
        self.attempt_customer = 0
        self.attempt_cpf = 0
        self.attempt_birth_date = 0

        # ── Formalization ──
        self.form_declines = 0
        self.form_accepted = False
        self.form_questions = 0

        # ── Backup ──
        self._saved_state = None
        self.save_state()

    # ── Propriedades ──

    @property
    def is_authenticated(self):
        return (
            self.validated_target_customer
            and (self.validated_cpf or self.validated_birth_date)
        )

    @property
    def attempts_exceeded(self):
        return any([
            self.attempt_customer >= 3,
            self.attempt_cpf >= 3,
            self.attempt_birth_date >= 3,
        ])

    # ── Save / Restore ──

    def save_state(self):
        state = copy.deepcopy(self.__dict__)
        state.pop("_saved_state", None)
        self._saved_state = state

    def restore_state(self):
        if self._saved_state is None:
            raise RuntimeError("Nenhum estado foi salvo ainda.")
        self.__dict__.update(copy.deepcopy(self._saved_state))

    # ── Debug ──

    def print_state(self):
        print(f"phase: {self.current_phase.value}")
        print(f"end_conversation: {self.end_conversation}")
        print(f"--- Presentation ---")
        print(f"  name_validation_attempt: {self.name_validation_attempt}")
        print(f"  is_target_customer: {self.is_target_customer}")
        print(f"--- Argumentation ---")
        print(f"  arg_declines: {self.arg_declines}")
        print(f"  arg_accepted_count: {self.arg_accepted_count}")
        print(f"  arg_accepted: {self.arg_accepted}")
        print(f"  arg_off_topic: {self.arg_off_topic}")
        print(f"--- Data Confirmation ---")
        print(f"  validated_target_customer: {self.validated_target_customer}")
        print(f"  validated_cpf: {self.validated_cpf}")
        print(f"  validated_birth_date: {self.validated_birth_date}")
        print(f"  is_authenticated: {self.is_authenticated}")
        print(f"--- Formalization ---")
        print(f"  form_declines: {self.form_declines}")
        print(f"  form_accepted: {self.form_accepted}")
        print(f"  form_questions: {self.form_questions}")
