# plugins/media/models.py

from app import db
from datetime import datetime

class Media(db.Model):
    __tablename__ = "media"
    __table_args__ = {"extend_existing": True}

    id = db.Column(db.Integer, primary_key=True)
    file_url = db.Column(db.String(256), nullable=False)
    uploaded_at = db.Column(db.DateTime, default=datetime.utcnow)
    uploader_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    caption = db.Column(db.String(255), nullable=True)

    plant_id = db.Column(db.Integer, db.ForeignKey("plant.id"), nullable=True)
    growlog_id = db.Column(db.Integer, db.ForeignKey("grow_logs.id"), nullable=True)
    update_id = db.Column(db.Integer, db.ForeignKey("plant_updates.id"), nullable=True)

    update = db.relationship("PlantUpdate", back_populates="media_items")

class ImageHeart(db.Model):
    __tablename__ = "image_hearts"
    __table_args__ = {"extend_existing": True}

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    media_id = db.Column(db.Integer, db.ForeignKey("media.id"), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    media = db.relationship("Media", backref="hearts")

class FeaturedImage(db.Model):
    __tablename__ = "featured_images"
    __table_args__ = {"extend_existing": True}

    id = db.Column(db.Integer, primary_key=True)
    media_id = db.Column(db.Integer, db.ForeignKey("media.id"), nullable=False)
    override_text = db.Column(db.String(255), nullable=True)
    is_featured = db.Column(db.Boolean, default=True)

    media = db.relationship("Media", backref="featured_entries")
