from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from datetime import datetime, timezone

db = SQLAlchemy()

# 用户表
class User(UserMixin, db.Model):
    __tablename__ = 'user'
    phone = db.Column(db.String(20), primary_key=True, comment="手机号码")
    name = db.Column(db.String(50), comment="姓名")
    branch = db.Column(db.String(50), comment="所属支局")
    role = db.Column(db.String(20), comment="角色")
    def get_id(self):
        return self.phone  # 使用手机号码作为唯一标识符

# 设备表
class Device(db.Model):
    __tablename__ = 'device'
    device_id = db.Column(db.String(100), primary_key=True, comment="设备编号")
    town = db.Column(db.String(50), comment="所属乡镇")
    branch = db.Column(db.String(50), comment="所属支局")
    unit_name = db.Column(db.String(100), comment="单位名称")
    project_type = db.Column(db.String(50), comment="项目类型")
    account = db.Column(db.String(20), comment="关联宽带账号")
    device_type = db.Column(db.String(50), comment="设备类型")
    puid = db.Column(db.String(100), comment="通道标识")  # 添加 puid 字段
    remark = db.Column(db.String(200), comment="备注")
    in_use = db.Column(db.Boolean, default=True, comment="是否在用")
    is_normal = db.Column(db.Boolean, default=True, comment="是否正常")

# 工单表
class WorkOrder(db.Model):
    __tablename__ = 'work_order'
    order_id = db.Column(db.String(20), primary_key=True, comment="工单号（日期+编号）")
    device_id = db.Column(db.String(100), db.ForeignKey('device.device_id'), comment="设备编号")  # 从20改为100
    dispatch_time = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), comment="派单时间")
    status = db.Column(db.String(20), default='待处理', comment="工单状态")
    branch = db.Column(db.String(50), comment="所属支局")
    # 与设备表的关系
    device = db.relationship('Device', backref='work_orders')

# 处理工单信息表
class WorkOrderHandling(db.Model):
    __tablename__ = 'work_order_handling'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    order_id = db.Column(db.String(20), db.ForeignKey('work_order.order_id'), comment="工单号")
    device_id = db.Column(db.String(100), db.ForeignKey('device.device_id'), comment="设备编号")  # 从20改为100
    branch = db.Column(db.String(50), comment="所属支局")
    restore_time = db.Column(db.DateTime, comment="恢复时间")
    fault_type = db.Column(db.String(100), comment="故障类型")
    contact_person = db.Column(db.String(50), comment="故障对接人")
    contact_phone = db.Column(db.String(20), comment="联系电话")
    handler = db.Column(db.String(50), comment="故障处理人")
    handle_description = db.Column(db.String(500), comment="处理描述")
    photo_paths = db.Column(db.String(500), comment="现场图片路径")
    distance_from_branch = db.Column(db.Float, comment="距离支局公里数")
    remark = db.Column(db.String(500), comment="备注")
    # 与工单表的关系
    work_order = db.relationship('WorkOrder', backref='handling_info')

# 手工故障单表
class ManualWorkOrder(db.Model):
    __tablename__ = 'manual_work_order'
    order_id = db.Column(db.String(20), primary_key=True, comment="工单号")
    project_type = db.Column(db.String(50), comment="项目类型")
    device_id = db.Column(db.String(100), comment="故障设备")
    device_location = db.Column(db.String(200), comment="设备位置")
    fault_time = db.Column(db.DateTime, comment="故障时间")
    handler = db.Column(db.String(50), comment="故障处理人")
    fault_type = db.Column(db.String(100), comment="故障类型")
    handle_description = db.Column(db.String(500), comment="处理描述")
    photo_paths = db.Column(db.String(500), comment="现场图片路径")
    contact_person = db.Column(db.String(50), comment="故障对接人")
    contact_phone = db.Column(db.String(20), comment="联系电话")
    distance_from_branch = db.Column(db.Float, comment="距离支局公里数")
    remark = db.Column(db.String(500), comment="备注")
    create_time = db.Column(db.DateTime, default=datetime.now, comment="创建时间")
    branch = db.Column(db.String(50), comment="所属支局")

class Channel(db.Model):
    __tablename__ = 'channels'
    
    puid = db.Column(db.String(50), primary_key=True)
    pu_name = db.Column(db.String(200))
    status = db.Column(db.String(20))
    update_time = db.Column(db.String(50))
    
    def __repr__(self):
        return f'<Channel {self.puid}>'
