"""Output validators for content quality validation.

These validators extend Pydantic schema validation by adding business logic
validation for summary and domain analysis results.
"""

from dataclasses import dataclass, field

from .models import DomainResult, SummaryResult


@dataclass
class ValidationResult:
    """Result of content validation.

    Attributes:
        valid: Whether validation passed
        errors: List of validation error messages
    """

    valid: bool
    errors: list[str] = field(default_factory=list)


class SummaryValidator:
    """Validator for SummaryResult content quality.

    Performs business logic validation beyond Pydantic schema validation:
    - main_findings count limit (max 10)
    - conclusions non-empty check
    - innovations items non-empty check
    """

    MAX_FINDINGS = 10

    def validate(self, result: SummaryResult) -> ValidationResult:
        """Validate SummaryResult content quality.

        Args:
            result: SummaryResult to validate

        Returns:
            ValidationResult with valid status and any error messages
        """
        errors: list[str] = []

        # Validate main_findings count (business rule: max 10)
        if len(result.main_findings) > self.MAX_FINDINGS:
            errors.append(
                f"Too many main findings: {len(result.main_findings)} "
                f"(max {self.MAX_FINDINGS})"
            )

        # Validate conclusions is not empty or whitespace-only
        if not result.conclusions.strip():
            errors.append("Conclusions cannot be empty or whitespace only")

        # Validate innovations items are non-empty if present
        for i, innovation in enumerate(result.innovations):
            if not innovation.strip():
                errors.append(f"Innovation item {i} cannot be empty")

        return ValidationResult(valid=len(errors) == 0, errors=errors)


class DomainValidator:
    """Validator for DomainResult content quality.

    Performs business logic validation beyond Pydantic schema validation:
    - primary_domain must be in relevant_domains
    - domain_scores values must be within 0-1 range
    - reasoning must not be empty
    """

    def validate(self, result: DomainResult) -> ValidationResult:
        """Validate DomainResult content quality.

        Args:
            result: DomainResult to validate

        Returns:
            ValidationResult with valid status and any error messages
        """
        errors: list[str] = []

        # Validate primary_domain is in relevant_domains
        if result.primary_domain not in result.relevant_domains:
            errors.append(
                f"Primary domain '{result.primary_domain}' "
                f"must be in relevant domains list"
            )

        # Validate domain_scores values are within 0-1 range
        for domain, score in result.domain_scores.items():
            if not 0 <= score <= 1:
                errors.append(
                    f"Domain score for '{domain}' ({score}) must be between 0 and 1"
                )

        # Validate reasoning is not empty or whitespace-only
        if not result.reasoning.strip():
            errors.append("Reasoning cannot be empty or whitespace only")

        return ValidationResult(valid=len(errors) == 0, errors=errors)


__all__ = ["DomainValidator", "SummaryValidator", "ValidationResult"]
