# plugins/submission/models.py

from datetime import datetime
from app import db

class Submission(db.Model):
    __tablename__ = "submissions"
    __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)
    submitted_at = db.Column(db.DateTime, default=datetime.utcnow)

    # Core fields
    plant_name = db.Column(db.String(100), nullable=True)  # now nullable
    scientific_name = db.Column(db.String(120), nullable=True)
    notes = db.Column(db.Text, nullable=True)

    submission_type = db.Column(db.String(50), nullable=False)

    # Market price fields
    price = db.Column(db.Float, nullable=True)
    source = db.Column(db.String(255), nullable=True)

    # Vendor fields
    vendor_name = db.Column(db.String(255), nullable=True)
    rating = db.Column(db.Integer, nullable=True)

    # Alias fields
    old_vendor = db.Column(db.String(255), nullable=True)
    new_vendor = db.Column(db.String(255), nullable=True)
    alias_reason = db.Column(db.Text, nullable=True)

    approved = db.Column(db.Boolean, default=None)
    approved_at = db.Column(db.DateTime, nullable=True)
    reviewed_by = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=True)

    # Relationships
    submitter = db.relationship(
        "User",
        foreign_keys=[user_id],
        back_populates="submitted_submissions",
        lazy=True
    )
    reviewer = db.relationship(
        "User",
        foreign_keys=[reviewed_by],
        back_populates="reviewed_submissions",
        lazy=True
    )

    images = db.relationship("SubmissionImage", backref="submission", lazy=True)


class SubmissionImage(db.Model):
    __tablename__ = "submission_images"
    __table_args__ = {"extend_existing": True}

    id = db.Column(db.Integer, primary_key=True)
    submission_id = db.Column(db.Integer, db.ForeignKey("submissions.id"), nullable=False)
    file_url = db.Column(db.String(256), nullable=False)
    uploaded_at = db.Column(db.DateTime, default=datetime.utcnow)
