from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class Song(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    artist = db.Column(db.String(150), default="Desconhecido")
    filename = db.Column(db.String(250), nullable=False)
    image_filename = db.Column(db.String(250), default="default_cover.png")
    duration = db.Column(db.Float, nullable=False) # Duration in seconds

class SongVote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    song_id = db.Column(db.Integer, db.ForeignKey('song.id'), nullable=False)
    user_ip = db.Column(db.String(50), nullable=False)
    vote_type = db.Column(db.String(10), nullable=False)  # like, dislike
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    __table_args__ = (
        db.UniqueConstraint('song_id', 'user_ip', name='uq_song_vote_song_ip'),
    )

class QueueItem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    song_id = db.Column(db.Integer, db.ForeignKey('song.id'), nullable=False)
    position = db.Column(db.Integer, default=0)
    song = db.relationship('Song')

class Recommendation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    artist = db.Column(db.String(150), nullable=False)
    email = db.Column(db.String(150), nullable=True)
    user_ip = db.Column(db.String(50), nullable=True)
    status = db.Column(db.String(20), default="pending") # pending, accepted, rejected
    rejection_reason = db.Column(db.String(250), nullable=True)

class BannedIP(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ip_address = db.Column(db.String(50), unique=True, nullable=False)
    reason = db.Column(db.String(250))
    banned_at = db.Column(db.DateTime, default=datetime.utcnow)

class BlogPost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    banner_filename = db.Column(db.String(250), nullable=True)
    description = db.Column(db.Text, nullable=False)
    links = db.Column(db.Text, nullable=True)  # JSON: [{"label": "...", "url": "..."}]
    published_at = db.Column(db.DateTime, default=datetime.utcnow)


class StationDetectionEvent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    station_key = db.Column(db.String(32), nullable=False, index=True)
    frequency = db.Column(db.Float, nullable=False, index=True)
    quality = db.Column(db.Float, nullable=False, default=0)
    source = db.Column(db.String(50), nullable=True)
    detected_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False, index=True)


class ListenerStat(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    count = db.Column(db.Integer, nullable=False)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False, index=True)


class PollSong(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    artist = db.Column(db.String(150), default="Desconhecido")
    votes = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)


class PollVote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    poll_song_id = db.Column(db.Integer, db.ForeignKey("poll_song.id"), nullable=False)
    user_ip = db.Column(db.String(50), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    __table_args__ = (
        db.UniqueConstraint("poll_song_id", "user_ip", name="uq_poll_vote_song_ip"),
    )


class ChatMessage(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_ip = db.Column(db.String(50), nullable=False, index=True)
    role = db.Column(db.String(10), nullable=False)  # 'user' or 'assistant'
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)

