from features.base_feature import BaseFeature


class GenericFeature(BaseFeature):
    """Fallback feature for any coordination type not matched by the 8 specific features."""

    async def get_context(self, preferences_a: dict, preferences_b: dict, user_a_id: int = None, user_b_id: int = None) -> str:
        goal_a = preferences_a.get("goal", "")
        goal_b = preferences_b.get("goal", "")

        lines = [
            "GENERIC COORDINATION RULES:",
            "• Find the solution that satisfies both parties' stated goals and hard constraints.",
            "• Be creative — there may be a win-win that isn't obvious from the positions stated.",
            "• Concede on nice-to-haves first, protect hard constraints at all costs.",
            "• If completely stuck, propose 2-3 concrete options for humans to choose from.",
        ]
        if goal_a:
            lines.append(f"\nPerson A's goal: {goal_a}")
        if goal_b:
            lines.append(f"Person B's goal: {goal_b}")

        return "\n".join(lines)

    def format_resolution(
        self, resolution: dict, preferences_a: dict, preferences_b: dict
    ) -> str:
        status = resolution.get("status", "resolved")
        final = resolution.get("final_proposal", {})
        details = final.get("details", {})
        rounds = resolution.get("rounds_taken", "?")
        summary = resolution.get("summary", "Agreement reached")

        if status == "escalated":
            return (
                f"⚠️ *Coordination — Human Decision Needed*\n\n"
                f"_{summary}_\n\n"
                f"Agents explored options but couldn't decide in {rounds} round(s)."
            )

        for_a = final.get("for_party_a") or details.get("for_a") or ""
        for_b = final.get("for_party_b") or details.get("for_b") or ""

        lines = ["✅ *Agreement Reached!*\n"]
        lines.append(f"_{summary}_")
        if for_a:
            lines.append(f"\n👤 *For you:* {for_a}")
        if for_b:
            lines.append(f"👤 *For them:* {for_b}")

        # Show key details generically
        filtered = {
            k: v for k, v in details.items()
            if k not in ("for_a", "for_b") and v
        }
        if filtered:
            lines.append("\n📋 *Details:*")
            for k, v in list(filtered.items())[:6]:
                lines.append(f"  • {k.replace('_', ' ').title()}: {v}")

        lines.append(f"\n⏱ Agreed in {rounds} round(s)")
        return "\n".join(lines)
