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


class PlantActivation(Base):
    __tablename__ = "plant_activations"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    activation_id: Mapped[str] = mapped_column(String(30), unique=True)
    passport_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_passports.id"))
    farmer_id: Mapped[str] = mapped_column(String(36), ForeignKey("farmer_profiles.id"))
    farm_id: Mapped[str] = mapped_column(String(36), ForeignKey("farms.id"))
    field_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("farm_fields.id"), nullable=True)
    purchase_date: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    activation_date: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    planting_date: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    initial_condition: Mapped[str | None] = mapped_column(String(50), nullable=True)
    initial_photo: Mapped[str | None] = mapped_column(String(500), nullable=True)
    gps_latitude: Mapped[float | None] = mapped_column(Float, nullable=True)
    gps_longitude: Mapped[float | None] = mapped_column(Float, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    status: Mapped[str] = mapped_column(String(20), default="activated")
    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())

    passport = relationship("PlantPassport", backref="activations")
    farmer = relationship("FarmerProfile", back_populates="plants")
    farm = relationship("Farm")
    field = relationship("FarmField")
    ownership_history = relationship("PlantOwnershipHistory", back_populates="activation", cascade="all, delete-orphan")
    growth_records = relationship("PlantGrowthRecord", back_populates="activation", cascade="all, delete-orphan")
    treatment_records = relationship("PlantTreatmentRecord", back_populates="activation", cascade="all, delete-orphan")
    health_records = relationship("PlantHealthRecord", back_populates="activation", cascade="all, delete-orphan")
    harvest_records = relationship("PlantHarvestRecord", back_populates="activation", cascade="all, delete-orphan")
    care_records = relationship("PlantCareRecord", back_populates="activation", cascade="all, delete-orphan")
    issue_reports = relationship("PlantIssueReport", back_populates="activation", cascade="all, delete-orphan")
    cost_records = relationship("PlantCostRecord", back_populates="activation", cascade="all, delete-orphan")
    care_schedules = relationship("PlantCareSchedule", back_populates="activation", cascade="all, delete-orphan")


class PlantOwnershipHistory(Base):
    __tablename__ = "plant_ownership_history"
    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id: Mapped[str] = mapped_column(String(36), ForeignKey("plant_passports.id"))
    activation_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("plant_activations.id"), nullable=True)
    from_owner_type: Mapped[str] = mapped_column(String(20))
    from_owner_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    to_owner_type: Mapped[str] = mapped_column(String(20))
    to_owner_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    transfer_date: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    transfer_reason: Mapped[str | None] = mapped_column(String(200), nullable=True)
    transfer_location: Mapped[str | None] = mapped_column(String(100), nullable=True)
    authorized_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="ownership_history")


class PlantCostRecord(Base):
    __tablename__ = "plant_cost_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"))
    cost_type: Mapped[str] = mapped_column(String(50))
    amount: Mapped[float] = mapped_column(Float, default=0.0)
    description: Mapped[str | None] = mapped_column(String(200), nullable=True)
    date: Mapped[datetime | None] = mapped_column(Date, nullable=True)
    reference_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
    reference_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

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