import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, Float, Integer, Text, ForeignKey, Date, Boolean, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base


class PlantGrowthRecord(Base):
    __tablename__ = "plant_growth_records"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    record_date: Mapped[datetime] = mapped_column(Date, server_default=func.now())
    plant_height_cm: Mapped[float | None] = mapped_column(Float, nullable=True)
    stem_diameter_mm: Mapped[float | None] = mapped_column(Float, nullable=True)
    canopy_size_cm: Mapped[float | None] = mapped_column(Float, nullable=True)
    leaf_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
    flower_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
    fruit_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
    health_score: Mapped[float | None] = mapped_column(Float, nullable=True)
    growth_stage: Mapped[str | None] = mapped_column(String(50), nullable=True)
    general_health: Mapped[str | None] = mapped_column(String(50), nullable=True)
    photo_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    recorded_by: Mapped[str | None] = mapped_column(String(36), ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    activation = relationship("PlantActivation", back_populates="growth_records")


class PlantTreatmentRecord(Base):
    __tablename__ = "plant_treatment_records"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    record_number: Mapped[str] = mapped_column(String(30), unique=True)
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    treatment_date: Mapped[datetime] = mapped_column(Date, server_default=func.now())
    treatment_type: Mapped[str] = mapped_column(String(50))
    product_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    active_ingredient: Mapped[str | None] = mapped_column(String(100), nullable=True)
    dosage: Mapped[str | None] = mapped_column(String(50), nullable=True)
    quantity: Mapped[float | None] = mapped_column(Float, nullable=True)
    unit: Mapped[str | None] = mapped_column(String(20), nullable=True)
    application_method: Mapped[str | None] = mapped_column(String(50), nullable=True)
    reason: Mapped[str | None] = mapped_column(Text, nullable=True)
    cost: Mapped[float] = mapped_column(Float, default=0.0)
    photo_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    recorded_by: Mapped[str | None] = mapped_column(String(36), ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    activation = relationship("PlantActivation", back_populates="treatment_records")


class PlantHealthRecord(Base):
    __tablename__ = "plant_health_records"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    record_date: Mapped[datetime] = mapped_column(Date, server_default=func.now())
    health_score: Mapped[float | None] = mapped_column(Float, nullable=True)
    plant_height_cm: Mapped[float | None] = mapped_column(Float, nullable=True)
    leaf_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
    stem_thickness_mm: Mapped[float | None] = mapped_column(Float, nullable=True)
    plant_color: Mapped[str | None] = mapped_column(String(50), nullable=True)
    root_status: Mapped[str | None] = mapped_column(String(50), nullable=True)
    moisture_level: Mapped[float | None] = mapped_column(Float, nullable=True)
    mortality_rate: Mapped[float | None] = mapped_column(Float, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    recorded_by: Mapped[str | None] = mapped_column(String(36), ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    activation = relationship("PlantActivation", back_populates="health_records")


class PlantHarvestRecord(Base):
    __tablename__ = "plant_harvest_records"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    harvest_number: Mapped[str] = mapped_column(String(30), unique=True)
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    harvest_date: Mapped[datetime] = mapped_column(Date, server_default=func.now())
    quantity: Mapped[float] = mapped_column(Float, default=0.0)
    unit: Mapped[str | None] = mapped_column(String(20), nullable=True)
    grade: Mapped[str | None] = mapped_column(String(20), nullable=True)
    quality: Mapped[str | None] = mapped_column(String(50), nullable=True)
    selling_price: Mapped[float | None] = mapped_column(Float, nullable=True)
    buyer: Mapped[str | None] = mapped_column(String(100), nullable=True)
    revenue: Mapped[float | None] = mapped_column(Float, nullable=True)
    photo_url: Mapped[str | None] = mapped_column(String(500), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    recorded_by: Mapped[str | None] = mapped_column(String(36), ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    activation = relationship("PlantActivation", back_populates="harvest_records")


class PlantCareRecord(Base):
    __tablename__ = "plant_care_records"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    care_type: Mapped[str] = mapped_column(String(50))
    care_date: Mapped[datetime] = mapped_column(Date, server_default=func.now())
    quantity: Mapped[float | None] = mapped_column(Float, nullable=True)
    unit: Mapped[str | None] = mapped_column(String(20), nullable=True)
    method: Mapped[str | None] = mapped_column(String(50), nullable=True)
    cost: Mapped[float] = mapped_column(Float, default=0.0)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    recorded_by: Mapped[str | None] = mapped_column(String(36), ForeignKey("users.id"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    activation = relationship("PlantActivation", back_populates="care_records")


class PlantCareSchedule(Base):
    __tablename__ = "plant_care_schedules"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    activation_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_activations.id"))
    care_type: Mapped[str] = mapped_column(String(50))  # watering, fertilizing, pruning, spraying, inspection
    frequency_days: Mapped[int] = mapped_column(Integer, default=1)
    next_due_date: Mapped[datetime] = mapped_column(Date)
    last_completed: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    reminder_time: Mapped[str | None] = mapped_column(String(10), nullable=True)  # "08:00"
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())

    activation = relationship("PlantActivation", back_populates="care_schedules")
