from datetime import datetime
from app import db
from plugins.plant.models import Plant


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)
    title = db.Column(db.String(255), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    updates = db.relationship('PlantUpdate', backref='grow_log', lazy=True)


class PlantUpdate(db.Model):
    __tablename__ = 'plant_updates'
    __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)
    growlog_id = db.Column(db.Integer, db.ForeignKey('grow_logs.id'), nullable=True)
    update_type = db.Column(db.String(50), nullable=False)
    description = db.Column(db.Text, nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    # Link to Media.update_id (must match the FK defined there)
    media_items = db.relationship('Media', back_populates='update', lazy=True)
