# plugins/plant/growlog/models.py

from datetime import datetime
from app import db

class GrowLog(db.Model):
    __tablename__   = "grow_logs"
    __table_args__  = {"extend_existing": True}

    id         = db.Column(db.Integer, primary_key=True)
    plant_id   = db.Column(db.Integer, db.ForeignKey("plant.id"), nullable=False)
    event_type = db.Column(db.String(50), nullable=False)
    title      = db.Column(db.String(255), nullable=True)
    notes      = db.Column(db.Text,      nullable=True)
    is_public  = db.Column(db.Boolean,   default=False, nullable=False)
    created_at = db.Column(db.DateTime,  default=datetime.utcnow, nullable=False)
    updated_at = db.Column(
        db.DateTime,
        default=datetime.utcnow,
        onupdate=datetime.utcnow,
        nullable=False
    )

    # ─── Single “primary” media for this log ───────────────────────────────────
    media_id = db.Column(db.Integer, db.ForeignKey("media.id"), nullable=True)
    media = db.relationship(
        "plugins.media.models.Media",
        backref=db.backref("update_images", lazy="dynamic"),
        foreign_keys=[media_id],
        lazy="joined",
    )

    # ─── All Media items whose growlog_id points here ─────────────────────────
    media_items = db.relationship(
        "plugins.media.models.Media",
        back_populates="growlog",
        foreign_keys="plugins.media.models.Media.growlog_id",
        lazy="dynamic",
        cascade="all, delete-orphan"
    )
