"""
File Models
"""
from sqlalchemy import Column, Integer, String, Text, ForeignKey, UniqueConstraint
from app.core.database import Base


class File(Base):
    """Files table"""
    __tablename__ = "files"
    
    id = Column(Text, primary_key=True)
    filename = Column(Text, nullable=False)
    original_filename = Column(Text, nullable=False)
    file_type = Column(Text)
    file_path = Column(Text, nullable=False)
    folder_path = Column(Text, default="")
    relative_path = Column(Text, default="")
    upload_date = Column(Text)
    file_size = Column(Integer)
    content_preview = Column(Text)
    full_content = Column(Text)
    image_path = Column(Text)
    thumbnail_path = Column(Text)


class Folder(Base):
    """Folders table"""
    __tablename__ = "folders"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    folder_name = Column(Text, nullable=False)
    folder_path = Column(Text, nullable=False)
    parent_path = Column(Text)
    full_path = Column(Text)
    file_count = Column(Integer, default=0)
    created_date = Column(Text)


class ExtractedImage(Base):
    """Extracted images table"""
    __tablename__ = "extracted_images"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    file_id = Column(Text, ForeignKey("files.id", ondelete="CASCADE"), nullable=False)
    image_path = Column(Text, nullable=False)
    image_type = Column(Text)
    page_number = Column(Integer)
    upload_date = Column(Text)


class SchedulerStatus(Base):
    """Global state for document ingestion scheduler"""
    __tablename__ = "scheduler_status"

    id = Column(Integer, primary_key=True, default=1)
    current_job_id = Column(Text, nullable=True)
    phase = Column(Text, nullable=False, default="active")
    active_seconds = Column(Integer, nullable=False, default=1200)
    rest_seconds = Column(Integer, nullable=False, default=600)
    phase_started_at = Column(Text, nullable=True)
    next_phase_at = Column(Text, nullable=True)


class FileSchedule(Base):
    """chronological File/Folder Task Queue"""
    __tablename__ = "file_schedules"

    id = Column(Integer, primary_key=True, autoincrement=True)
    file_path = Column(Text, nullable=False)
    agent_id = Column(Text, nullable=True)
    job_id = Column(Text, nullable=True)
    root_path = Column(Text, nullable=True)
    status = Column(Text, nullable=False, default="pending")  # 'pending', 'processing', 'completed', 'failed'
    created_at = Column(Text, nullable=True)
