"""SQLAlchemy ORM models — MySQL 5.7+/MariaDB 10.3+ compatible."""
from __future__ import annotations

from sqlalchemy import (
    Column, String, Integer, Boolean, Text, DateTime, JSON, Index,
)
from sqlalchemy.orm import declarative_base

Base = declarative_base()


class User(Base):
    __tablename__ = "users"
    id = Column(String(64), primary_key=True)
    username = Column(String(64), nullable=False, unique=True)
    password_hash = Column(String(255), nullable=False)
    role = Column(String(16), nullable=False, default="user")         # 'admin' | 'user'
    created_at = Column(String(40), nullable=False)                   # ISO-8601 UTC
    expiry_at = Column(String(40), nullable=True)
    xtream_mode = Column(String(16), nullable=False, default="shared")  # 'shared' | 'own'
    xtream_server = Column(String(500), nullable=False, default="")
    xtream_username = Column(String(255), nullable=False, default="")
    xtream_password = Column(String(255), nullable=False, default="")
    note = Column(Text, nullable=False, default="")

    __table_args__ = (Index("ix_users_role", "role"),)

    def as_dict(self) -> dict:
        return {
            "id": self.id,
            "username": self.username,
            "password_hash": self.password_hash,
            "role": self.role,
            "created_at": self.created_at,
            "expiry_at": self.expiry_at,
            "xtream_mode": self.xtream_mode,
            "xtream_server": self.xtream_server or "",
            "xtream_username": self.xtream_username or "",
            "xtream_password": self.xtream_password or "",
            "note": self.note or "",
        }


class Device(Base):
    __tablename__ = "devices"
    device_id = Column(String(32), primary_key=True)
    mac_address = Column(String(32), nullable=False)
    pin = Column(String(8), nullable=False, unique=True)
    paired = Column(Boolean, nullable=False, default=False)
    server_url = Column(String(500), nullable=True)
    username = Column(String(255), nullable=True)
    password = Column(String(255), nullable=True)
    profile_name = Column(String(255), nullable=True)
    created_at = Column(String(40), nullable=False)
    paired_at = Column(String(40), nullable=True)

    def as_dict(self) -> dict:
        return {
            "device_id": self.device_id,
            "mac_address": self.mac_address,
            "pin": self.pin,
            "paired": bool(self.paired),
            "server_url": self.server_url,
            "username": self.username,
            "password": self.password,
            "profile_name": self.profile_name,
            "created_at": self.created_at,
            "paired_at": self.paired_at,
        }


class Setting(Base):
    """Singleton settings row.  id = 'global'.  All flexible fields live in `data` JSON."""
    __tablename__ = "settings"
    id = Column(String(32), primary_key=True)
    data = Column(JSON, nullable=False)


class StatusCheck(Base):
    __tablename__ = "status_checks"
    id = Column(String(64), primary_key=True)
    client_name = Column(String(255), nullable=False)
    timestamp = Column(String(40), nullable=False)
