from datetime import datetime
# 引入 UserMixin 是 Flask 标准做法
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from extensions import db


# =======================
# 数据库 1: 业务数据 (默认数据库 / devices.db)
# =======================

class Device(db.Model):
    __tablename__ = 'devices'
    # 默认数据库，无需 bind_key

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, index=True)
    source = db.Column(db.String(50))

    # 快照字段（爬虫自动更新）
    status = db.Column(db.String(50))
    current_value = db.Column(db.String(200))
    latest_time = db.Column(db.String(50))
    json_data = db.Column(db.Text)  # 存储完整原始JSON
    check_time = db.Column(db.String(50))
    reason = db.Column(db.String(255))
    offset = db.Column(db.String(50))  # 时间偏移量说明

    # 手动录入字段 (管理员/工程师可改)
    install_site = db.Column(db.String(100), default="")
    is_maintaining = db.Column(db.Boolean, default=False)
    is_hidden = db.Column(db.Boolean, default=False)

    # 🟢 [新增] 维修人字段 (对应你在数据库中新加的列)
    maintainer = db.Column(db.String(50))

    def to_dict(self):
        """
        转换为前端友好的字典格式
        """
        # 简单处理状态：只要不是明确的离线/异常，就视为 online
        api_status = 'offline' if self.status in ['离线', '异常', '已离线'] else 'online'

        return {
            'id': self.id,
            'name': self.name,
            'source': self.source,
            'latest_time': self.latest_time,
            'status': api_status,  # 给前端图标用的状态 (online/offline)
            'status_text': self.status,  # 显示在界面的原始状态文字
            'value': self.current_value,
            'reason': self.reason,
            'install_site': self.install_site or '',
            'is_maintaining': self.is_maintaining,
            'is_hidden': self.is_hidden,
            'offset': self.offset,
            # 🟢 [新增] 返回维修人给前端显示
            'maintainer': self.maintainer
        }


class DeviceHistory(db.Model):
    __tablename__ = 'device_history'

    id = db.Column(db.Integer, primary_key=True)
    device_id = db.Column(db.Integer, db.ForeignKey('devices.id'))

    data_time = db.Column(db.String(50))
    status = db.Column(db.String(50))
    result_data = db.Column(db.String(200), default="")
    json_data = db.Column(db.Text)
    file_path = db.Column(db.String(255))
    recorded_at = db.Column(db.DateTime, default=datetime.now)


class MaintenanceLog(db.Model):
    __tablename__ = 'maintenance_logs'

    id = db.Column(db.Integer, primary_key=True)
    device_name = db.Column(db.String(100), nullable=False)
    engineer = db.Column(db.String(50))
    location = db.Column(db.String(100))
    content = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, default=datetime.now)

    def to_dict(self):
        return {
            'id': self.id,
            'device_name': self.device_name,
            'engineer': self.engineer or '',
            'location': self.location or '',
            'content': self.content,
            'timestamp': self.timestamp.strftime('%Y-%m-%d %H:%M:%S')
        }


# =======================
# 数据库 2: 用户管理 (users.db)
# =======================

class User(UserMixin, db.Model):
    __bind_key__ = 'users_db'  # 指定存储在 users.db
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))

    # 角色: 'admin', 'engineer', 'client'
    role = db.Column(db.String(20), default='client')
    created_at = db.Column(db.DateTime, default=datetime.now)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)


class UserDevicePermission(db.Model):
    __bind_key__ = 'users_db'
    __tablename__ = 'user_device_permissions'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    device_id = db.Column(db.Integer, nullable=False)