import enum
import uuid
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional

from sqlalchemy import DateTime, Enum as SAEnum, Float, Integer, JSON, String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base


class VarietyType(enum.Enum):
    scion = "scion"
    rootstock = "rootstock"
    both = "both"


class RootDepthCategory(enum.Enum):
    shallow = "shallow"
    medium = "medium"
    deep = "deep"


class Plant(Base):
    __tablename__ = "plants"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    species: Mapped[str] = mapped_column(String(255), nullable=False)
    variety_type: Mapped[VarietyType] = mapped_column(
        SAEnum(VarietyType, name="varietytype"), nullable=False
    )
    drought_tolerance: Mapped[int] = mapped_column(Integer, nullable=False)
    salinity_tolerance: Mapped[int] = mapped_column(Integer, nullable=False)
    heat_tolerance: Mapped[int] = mapped_column(Integer, nullable=False)
    cold_tolerance: Mapped[int] = mapped_column(Integer, nullable=False)
    disease_resistance: Mapped[Dict[str, Any]] = mapped_column(JSON, default=dict)
    optimal_ph_min: Mapped[float] = mapped_column(Float, nullable=False)
    optimal_ph_max: Mapped[float] = mapped_column(Float, nullable=False)
    nutrient_uptake_efficiency: Mapped[Dict[str, Any]] = mapped_column(JSON, default=dict)
    root_depth_category: Mapped[RootDepthCategory] = mapped_column(
        SAEnum(RootDepthCategory, name="rootdepthcategory"), nullable=False
    )
    growth_cycle_days: Mapped[int] = mapped_column(Integer, nullable=False)
    yield_potential_kg_per_plant: Mapped[float] = mapped_column(Float, nullable=False)
    description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    image_url: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc),
    )

    rootstock_combinations: Mapped[List["GraftCombination"]] = relationship(  # type: ignore[name-defined]
        "GraftCombination",
        foreign_keys="[GraftCombination.rootstock_id]",
        back_populates="rootstock",
    )
    scion_combinations: Mapped[List["GraftCombination"]] = relationship(  # type: ignore[name-defined]
        "GraftCombination",
        foreign_keys="[GraftCombination.scion_id]",
        back_populates="scion",
    )
    ungrafted_scenarios: Mapped[List["Scenario"]] = relationship(  # type: ignore[name-defined]
        "Scenario",
        foreign_keys="[Scenario.plant_id]",
        back_populates="plant",
    )
