from sqlalchemy import (
    Column, Integer, String, Enum, TIMESTAMP, func, Boolean, ForeignKey, Float, Text, Index
)
from sqlalchemy.orm import declarative_base
import enum

Base = declarative_base()


class UserRole(enum.Enum):
    user = "user"
    admin = "admin"
    superadmin = "superadmin"


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(50), unique=True, nullable=False, index=True)
    password_hash = Column(String(128), nullable=False)
    role = Column(Enum(UserRole), nullable=False, default=UserRole.user)
    is_active = Column(Boolean, default=True, nullable=False)
    created_at = Column(TIMESTAMP(timezone=True),
                        server_default=func.current_timestamp())
    updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
    ), onupdate=func.current_timestamp())


class ClientGroup(Base):
    __tablename__ = 'client_groups'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), unique=True, nullable=False)
    created_at = Column(TIMESTAMP(timezone=True),
                        server_default=func.current_timestamp())
    is_active = Column(Boolean, default=True, nullable=False)


class Client(Base):
    __tablename__ = 'clients'
    uuid = Column(String(36), primary_key=True, nullable=False)
    hardware_hash = Column(String(64), nullable=False, index=True)
    location = Column(String(100), nullable=True)
    ip_address = Column(String(45), nullable=True, index=True)
    registration_time = Column(TIMESTAMP(
        timezone=True), server_default=func.current_timestamp(), nullable=False)
    last_alive = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
    ), onupdate=func.current_timestamp(), nullable=False)
    is_active = Column(Boolean, default=True, nullable=False)
    group_id = Column(Integer, ForeignKey(
        'client_groups.id'), nullable=False, default=1)


class EventType(enum.Enum):
    presentation = "presentation"
    website = "website"
    video = "video"
    message = "message"
    other = "other"
    webuntis = "webuntis"


class MediaType(enum.Enum):
    # Präsentationen
    pdf = "pdf"
    ppt = "ppt"
    pptx = "pptx"
    odp = "odp"
    # Videos (gängige VLC-Formate)
    mp4 = "mp4"
    avi = "avi"
    mkv = "mkv"
    mov = "mov"
    wmv = "wmv"
    flv = "flv"
    webm = "webm"
    mpg = "mpg"
    mpeg = "mpeg"
    ogv = "ogv"
    # Bilder (benutzerfreundlich)
    jpg = "jpg"
    jpeg = "jpeg"
    png = "png"
    gif = "gif"
    bmp = "bmp"
    tiff = "tiff"
    svg = "svg"
    # HTML-Mitteilung
    html = "html"


class Event(Base):
    __tablename__ = 'events'
    id = Column(Integer, primary_key=True, autoincrement=True)
    group_id = Column(Integer, ForeignKey(
        'client_groups.id'), nullable=False, index=True)
    title = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    start = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
    end = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
    event_type = Column(Enum(EventType), nullable=False)
    event_media_id = Column(Integer, ForeignKey(
        'event_media.id'), nullable=True)
    autoplay = Column(Boolean, nullable=True)            # NEU
    loop = Column(Boolean, nullable=True)                # NEU
    volume = Column(Float, nullable=True)                # NEU
    slideshow_interval = Column(Integer, nullable=True)  # NEU
    created_at = Column(TIMESTAMP(timezone=True),
                        server_default=func.current_timestamp())
    updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
    ), onupdate=func.current_timestamp())
    created_by = Column(Integer, ForeignKey('users.id'), nullable=False)
    updated_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    is_active = Column(Boolean, default=True, nullable=False)


class EventMedia(Base):
    __tablename__ = 'event_media'
    id = Column(Integer, primary_key=True, autoincrement=True)
    media_type = Column(Enum(MediaType), nullable=False)
    url = Column(String(255), nullable=False)
    file_path = Column(String(255), nullable=True)
    message_content = Column(Text, nullable=True)

    def to_dict(self):
        return {
            "id": self.id,
            "media_type": self.media_type.value if self.media_type else None,
            "url": self.url,
            "file_path": self.file_path,
            "message_content": self.message_content,
        }
