from __future__ import annotations

from dataclasses import dataclass, field
from typing import Literal


@dataclass
class QuranVerse:
    """Represents a single verse (ayah) from the Quran."""
    id: int
    surah: int
    ayah: int
    text: str
    text_simple: str
    page: int
    juz: int


@dataclass
class QuranSurah:
    """Represents a Surah (chapter) of the Quran."""
    number: int
    name: str
    english_name: str
    verses_count: int
    revelation_type: Literal["Meccan", "Medinan"]


MatchType = Literal["exact", "normalized", "none"]


@dataclass
class ValidationResult:
    """Result of validating a potential Quran quote."""
    is_valid: bool
    match_type: MatchType
    matched_verse: QuranVerse | None = None
    reference: str | None = None
    normalized_input: str | None = None
    expected_normalized: str | None = None
    mismatch_index: int | None = None
    suggestions: list[dict] | None = None  # list of {"verse": QuranVerse, "reference": str}


@dataclass
class ArabicSegment:
    """An extracted Arabic text segment with position info."""
    text: str
    start_index: int
    end_index: int


@dataclass
class DetectionSegment:
    """A detected segment with optional validation."""
    text: str
    start_index: int
    end_index: int
    validation: ValidationResult | None = None


@dataclass
class DetectionResult:
    """Detection result for finding Quran quotes in text."""
    detected: bool
    segments: list[DetectionSegment] = field(default_factory=list)


@dataclass
class ValidatorOptions:
    """Options for the validator."""
    max_suggestions: int = 3
    min_detection_length: int = 10


@dataclass
class WordAnalysis:
    """Analysis of a single word."""
    word: str
    is_fabricated: bool


@dataclass
class FabricationStats:
    """Summary stats for fabrication analysis."""
    total_words: int
    fabricated_words: int
    fabricated_ratio: float


@dataclass
class FabricationAnalysis:
    """Analysis of word-level fabrication in text."""
    normalized_input: str
    words: list[WordAnalysis] = field(default_factory=list)
    stats: FabricationStats = field(default_factory=lambda: FabricationStats(0, 0, 0.0))


@dataclass
class QuoteAnalysis:
    """Analysis of a single Quran quote."""
    original: str
    corrected: str
    is_valid: bool
    reference: str | None = None
    detection_method: Literal["tagged", "contextual", "fuzzy"] = "tagged"
    start_index: int = 0
    end_index: int = 0
    was_corrected: bool = False
    normalized_input: str | None = None
    expected_normalized: str | None = None
    fabrication_analysis: FabricationAnalysis | None = None


@dataclass
class ProcessedOutput:
    """Result of processing LLM output for Quran validation."""
    corrected_text: str
    all_valid: bool
    quotes: list[QuoteAnalysis] = field(default_factory=list)
    warnings: list[str] = field(default_factory=list)


@dataclass
class LLMProcessorOptions:
    """Options for the LLM processor."""
    auto_correct: bool = True
    scan_untagged: bool = True
    tag_format: Literal["xml", "markdown", "bracket"] = "xml"
