from __future__ import annotations

from rdkit import Chem

from ._core import ensure_mol, find_macrolactone_candidates


class MacroLactoneAnalyzer:
    """Identify valid 12-20 membered macrolactones."""

    def get_valid_ring_sizes(self, mol_input: str | Chem.Mol) -> list[int]:
        mol, _ = ensure_mol(mol_input)
        candidates = find_macrolactone_candidates(mol)
        return sorted({candidate.ring_size for candidate in candidates})

    def analyze_molecule(self, mol_input: str | Chem.Mol) -> dict:
        mol, smiles = ensure_mol(mol_input)
        candidates = find_macrolactone_candidates(mol)
        valid_ring_sizes = sorted({candidate.ring_size for candidate in candidates})
        is_ambiguous = len(valid_ring_sizes) > 1 or len(candidates) > 1
        return {
            "smiles": smiles,
            "valid_ring_sizes": valid_ring_sizes,
            "candidate_count": len(candidates),
            "is_ambiguous": is_ambiguous,
            "selected_ring_size": valid_ring_sizes[0] if len(valid_ring_sizes) == 1 and len(candidates) == 1 else None,
        }
