from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
from sqlalchemy.orm import relationship
from datetime import datetime
from ...shared.config.database import Base

class Banner(Base):
    __tablename__ = 'banners'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    title = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    image_url = Column(String(255), nullable=False)
    link_url = Column(String(255), nullable=True)
    position = Column(String(50), nullable=False, default='home')
    display_order = Column(Integer, nullable=False, default=0)
    is_active = Column(Boolean, nullable=False, default=True)
    start_date = Column(DateTime, nullable=True)
    end_date = Column(DateTime, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

    def is_active_now(self):
        from datetime import datetime
        now = datetime.utcnow()
        if not self.is_active:
            return False
        if not self.start_date or not self.end_date:
            return self.is_active
        return self.start_date <= now <= self.end_date