from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

from config import db

class RecentlyWatched(db.Model):
    __tablename__ = 'recently_watched'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False)
    video_id = db.Column(db.Integer, db.ForeignKey('videos.id'), nullable=False)
    course_id = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(1000), nullable=False)
    time_watched = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    duration_watched = db.Column(db.Integer, default=0, comment='Duration in seconds')
    watched_percentage = db.Column(db.Numeric(5, 2), default=0.00, comment='Percentage of video watched')
    device = db.Column(db.String(100), default=None, comment='Device type (e.g., mobile, desktop)')
    platform = db.Column(db.String(100), default=None, comment='Platform (e.g., iOS, Android)')
    session_id = db.Column(db.String(255), default=None, comment='Session identifier for tracking user sessions')
    ip_address = db.Column(db.String(45), default=None, comment='IP address of the user')
    user_agent = db.Column(db.String(2000), default=None, comment='User agent string for tracking browser details')

    # Relationship to the Video table
    video = db.relationship('Video', backref='recently_watched', lazy=True)

    def __repr__(self):
        return f'<RecentlyWatched user_id={self.user_id}, course_id={self.course_id}, title={self.title}>'


from datetime import datetime

class Video(db.Model):
    __tablename__ = 'videos'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    video_url = db.Column(db.String(500), nullable=False)
    type = db.Column(db.String(1000), nullable=False)
    description = db.Column(db.Text, default=None)
    credit = db.Column(db.String(255), nullable=False)
    for_which = db.Column(db.String(100), nullable=True)
    level = db.Column(db.Enum('beginner', 'intermediate', 'advanced', name='level_enum'), nullable=False)
    image_url = db.Column(db.String(500), nullable=False)
    video_duration = db.Column(db.Time, nullable=False)
    count_exercises = db.Column(db.Integer, default=0)
    count_students = db.Column(db.Integer, default=0)
    topic_id = db.Column(db.Integer, default=1)
    count_plays = db.Column(db.Integer, default=0)
    count_views = db.Column(db.Integer, default=0)
    count_likes = db.Column(db.Integer, default=0)
    count_comments = db.Column(db.Integer, default=0)
    short_description = db.Column(db.String(500), nullable=False)
    course_id = db.Column(db.Integer, nullable=False)
    is_live = db.Column(db.Boolean, default=False)
    chat_id = db.Column(db.String(255), nullable=False)
    is_active = db.Column(db.Boolean, default=True)
    free = db.Column(db.Boolean, default=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        # Convert time fields to string
        video_duration_str = self.video_duration.strftime('%H:%M:%S') if self.video_duration else None
        created_at_str = self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        updated_at_str = self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None

        return {
            column.name: getattr(self, column.name) if column.name != 'video_duration' else video_duration_str
            for column in self.__table__.columns
        }
