# File: plugins/utility/models.py

from datetime import datetime
from app import db

class ImportBatch(db.Model):
    __tablename__ = 'import_batches'

    id          = db.Column(db.Integer, primary_key=True)
    export_id   = db.Column(db.String(64), nullable=False)
    user_id     = db.Column(
        db.Integer,
        db.ForeignKey('users.id', ondelete='CASCADE'),
        nullable=False,
        index=True
    )
    imported_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    # New columns to track background processing status
    status      = db.Column(
        db.String(20),
        nullable=False,
        default='pending',
        doc="One of: pending, processing, done, failed"
    )
    error       = db.Column(
        db.Text,
        nullable=True,
        doc="If status=='failed', the exception text"
    )

    __table_args__ = (
        # ensure a given user can’t import the same export twice
        db.UniqueConstraint('export_id', 'user_id', name='uix_export_user'),
    )

    def __repr__(self):
        return (
            f"<ImportBatch id={self.id!r} export_id={self.export_id!r} "
            f"user_id={self.user_id!r} status={self.status!r}>"
        )
