"""
Tool_OCR - Export Rule Model
User-defined export rules and formatting configurations
"""

from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, JSON
from sqlalchemy.orm import relationship
from datetime import datetime

from app.core.database import Base


class ExportRule(Base):
    """Export rule configuration for customized output formatting"""

    __tablename__ = "paddle_ocr_export_rules"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("paddle_ocr_users.id", ondelete="CASCADE"), nullable=False, index=True)
    rule_name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)

    # Rule configuration stored as JSON
    # {
    #   "filters": {
    #     "confidence_threshold": 0.8,
    #     "filename_pattern": "invoice_*",
    #     "language": "ch"
    #   },
    #   "formatting": {
    #     "add_line_numbers": true,
    #     "sort_by_position": true,
    #     "group_by_filename": false
    #   },
    #   "export_options": {
    #     "include_metadata": true,
    #     "include_confidence": true,
    #     "include_bounding_boxes": false
    #   }
    # }
    config_json = Column(JSON, nullable=False)

    # CSS template for PDF export (optional)
    # Can reference predefined templates: "default", "academic", "business", "report"
    # Or store custom CSS
    css_template = Column(Text, nullable=True)

    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

    # Relationships
    user = relationship("User", back_populates="export_rules")

    def __repr__(self):
        return f"<ExportRule(id={self.id}, name='{self.rule_name}', user_id={self.user_id})>"
