"""Core types for DentalComplianceOS."""

from __future__ import annotations

import datetime
from dataclasses import dataclass, field, asdict
from enum import Enum
from typing import Any


class Severity(str, Enum):
    """Severity levels for compliance findings."""
    CRITICAL = "critical"
    HIGH = "high"
    MEDIUM = "medium"
    LOW = "low"
    INFO = "info"


class ComplianceStatus(str, Enum):
    """Status of a compliance check."""
    COMPLIANT = "compliant"
    NON_COMPLIANT = "non_compliant"
    PARTIAL = "partial"
    UNKNOWN = "unknown"


class PracticeType(str, Enum):
    """Type of dental practice."""
    SOLO_GP = "solo_gp"
    GROUP = "group"
    ORTHO = "ortho"
    PERIO = "perio"
    ORAL_SURGERY = "oral_surgery"
    PEDO = "pedo"
    ENDO = "endo"
    PROSTHO = "prostho"
    DSO = "dso"


class CycleType(str, Enum):
    """Autoclave sterilization cycle types."""
    GRAVITY = "gravity"
    PRE_VACUUM = "pre_vacuum"
    FLASH = "flash"


class RadiationLogType(str, Enum):
    """Types of radiation log entries."""
    EXPOSURE = "exposure"
    QA_TEST = "qa_test"
    EQUIPMENT_CHECK = "equipment_check"
    LEAD_APRON_CHECK = "lead_apron_check"


@dataclass
class ComplianceCheck:
    """A single compliance check definition."""

    id: str
    title: str
    description: str
    regulation_reference: str
    severity: str
    remediation_steps: list[str]
    scanner: str = "practice"
    auto_fix: bool = False
    practice_types: list[str] = field(default_factory=lambda: ["all"])

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        return asdict(self)


@dataclass
class ComplianceFinding:
    """A single finding from a compliance scan."""

    framework: str
    check_id: str
    title: str
    status: ComplianceStatus
    severity: Severity
    detail: str = ""
    evidence: str = ""
    corrective_action: str = ""
    responsible_person: str = ""
    last_checked: str = field(
        default_factory=lambda: datetime.datetime.utcnow().isoformat()
    )
    next_due: str = ""
    notes: str = ""

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        d = asdict(self)
        d["status"] = self.status.value
        d["severity"] = self.severity.value
        return d


@dataclass
class SterilizationRecord:
    """A sterilization cycle log entry."""

    autoclave_id: str
    cycle_type: str
    temperature: float
    pressure: float
    duration_minutes: float
    biological_indicator: str  # pass, fail, pending
    chemical_indicator: str  # pass, fail
    load_contents: str = ""
    operator: str = ""
    notes: str = ""

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        return asdict(self)


@dataclass
class RadiationRecord:
    """A radiation log entry."""

    equipment_id: str
    log_type: str
    details: str
    result: str = ""
    operator: str = ""

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        return asdict(self)


@dataclass
class ScanResult:
    """Aggregated result of a dental compliance scan."""

    scan_id: str
    framework: str
    practice_type: str
    state: str
    timestamp: str
    total_checks: int = 0
    compliant: int = 0
    non_compliant: int = 0
    partial: int = 0
    unknown: int = 0
    score: float = 0.0
    findings: list[ComplianceFinding] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        return {
            "scan_id": self.scan_id,
            "framework": self.framework,
            "practice_type": self.practice_type,
            "state": self.state,
            "timestamp": self.timestamp,
            "total_checks": self.total_checks,
            "compliant": self.compliant,
            "non_compliant": self.non_compliant,
            "partial": self.partial,
            "unknown": self.unknown,
            "score": self.score,
            "findings": [f.to_dict() for f in self.findings],
        }
