from datetime import datetime
from app import db

class TransferRequest(db.Model):
    __tablename__ = 'transfer_request'
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    plant_id = db.Column(db.Integer, db.ForeignKey('plant.id'), nullable=False)
    seller_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    buyer_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    status = db.Column(
        db.String(20),
        nullable=False,
        default='pending'
    )
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
    seller_message = db.Column(db.String(512), nullable=True)
    buyer_message = db.Column(db.String(512), nullable=True)

    plant = db.relationship(
        'plugins.plant.models.Plant',
        backref=db.backref('transfer_requests', lazy='dynamic'),
        lazy=True
    )
    seller = db.relationship(
        'plugins.auth.models.User',
        foreign_keys=[seller_id],
        backref='outgoing_transfers',
        lazy=True
    )
    buyer = db.relationship(
        'plugins.auth.models.User',
        foreign_keys=[buyer_id],
        backref='incoming_transfers',
        lazy=True
    )

    def __repr__(self):
        return f"<TransferRequest id={self.id} plant={self.plant_id} seller={self.seller_id} buyer={self.buyer_id} status={self.status}>"
