"""
models.py

SQLAlchemy ORM models for LeafDoctor AI.
"""

from datetime import datetime

from sqlalchemy import (
    Column,
    DateTime,
    Float,
    ForeignKey,
    Integer,
    String,
    Text,
)
from sqlalchemy.orm import relationship

from database import Base


class ProtectionLabel(Base):
    """
    Master disease/problem labels.
    """

    __tablename__ = "protection_labels"

    id = Column(Integer, primary_key=True, index=True)

    class_id = Column(Integer, unique=True, nullable=False, index=True)

    label_name_en = Column(String(255), nullable=False)
    label_name_bn = Column(String(255), nullable=True)

    primary_category_en = Column(String(255), nullable=True)
    primary_category_bn = Column(String(255), nullable=True)

    created_at = Column(
        DateTime,
        default=datetime.utcnow,
        nullable=False,
    )

    treatments = relationship(
        "TreatmentMapping",
        back_populates="label",
        cascade="all, delete-orphan",
        lazy="selectin",
    )

    def __repr__(self):
        return (
            f"<ProtectionLabel("
            f"class_id={self.class_id}, "
            f"label='{self.label_name_en}')>"
        )


class TreatmentMapping(Base):
    """
    Crop specific recommendation table.
    """

    __tablename__ = "treatment_mapping"

    id = Column(Integer, primary_key=True, index=True)

    class_id = Column(
        Integer,
        ForeignKey("protection_labels.class_id"),
        nullable=False,
        index=True,
    )

    crop_en = Column(String(150), nullable=False)
    crop_bn = Column(String(150), nullable=True)

    problem_type_en = Column(String(255), nullable=True)
    problem_type_bn = Column(String(255), nullable=True)

    chemical = Column(Text, nullable=True)
    chemical_type = Column(String(255), nullable=True)

    dosage = Column(Text, nullable=True)

    sms_en = Column(Text, nullable=True)
    sms_bn = Column(Text, nullable=True)

    created_at = Column(
        DateTime,
        default=datetime.utcnow,
        nullable=False,
    )

    label = relationship(
        "ProtectionLabel",
        back_populates="treatments",
        lazy="joined",
    )

    diagnoses = relationship(
        "DiagnosisHistory",
        back_populates="treatment",
        lazy="selectin",
    )

    def __repr__(self):
        return (
            f"<TreatmentMapping("
            f"class_id={self.class_id}, "
            f"crop='{self.crop_en}')>"
        )


class DiagnosisHistory(Base):
    """
    Diagnosis history.
    """

    __tablename__ = "diagnosis_history"

    id = Column(Integer, primary_key=True, index=True)

    image_path = Column(String(500), nullable=False)

    crop_detected = Column(String(255), nullable=False)

    predicted_label = Column(String(255), nullable=False)

    class_id = Column(Integer, nullable=False)

    confidence = Column(Float, nullable=False)

    treatment_id = Column(
        Integer,
        ForeignKey("treatment_mapping.id"),
        nullable=True,
        index=True,
    )

    gemini_model = Column(String(100), nullable=False)

    raw_response = Column(Text, nullable=True)

    verified_label = Column(String(255), nullable=True)
    verified_by = Column(String(255), nullable=True)
    verified_at = Column(DateTime, nullable=True)

    created_at = Column(
        DateTime,
        default=datetime.utcnow,
        nullable=False,
    )

    treatment = relationship(
        "TreatmentMapping",
        back_populates="diagnoses",
        lazy="joined",
    )

    def __repr__(self):
        return (
            f"<DiagnosisHistory("
            f"id={self.id}, "
            f"class_id={self.class_id}, "
            f"confidence={self.confidence})>"
        )