# -*- coding: utf-8 -*-
"""
PLT处理记录和用户操作日志模型
"""

from sqlalchemy import Column, Integer, String, DateTime, func, ForeignKey, Text, JSON, Float
from sqlalchemy.orm import relationship
from app.db import Base


class PltProcessRecord(Base):
    """PLT处理记录表"""
    __tablename__ = "plt_process_records"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    
    # 文件信息
    filename = Column(String(255), nullable=True)
    file_size = Column(Integer, nullable=True)  # 字节
    rotated_filename = Column(String(255), nullable=True)  # 旋转对比文件名
    
    # 处理参数
    size_labels = Column(JSON, nullable=True)  # ["S", "M", "L", ...]
    dpi = Column(Integer, default=150)
    rotation = Column(Integer, default=0)
    
    # 处理结果
    total_groups = Column(Integer, default=0)  # 裁片组数
    total_pieces = Column(Integer, default=0)  # 裁片总数
    process_time_ms = Column(Integer, nullable=True)  # 处理耗时(毫秒)
    
    # 状态
    status = Column(String(20), default='success')  # success, error
    error_message = Column(Text, nullable=True)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    
    # 关联
    user = relationship("User", backref="plt_records")
    pieces = relationship("PltPiece", back_populates="record", cascade="all, delete-orphan")


class PltPiece(Base):
    """PLT裁片详情表"""
    __tablename__ = "plt_pieces"
    
    id = Column(Integer, primary_key=True, index=True)
    record_id = Column(Integer, ForeignKey("plt_process_records.id", ondelete="CASCADE"), nullable=False, index=True)
    
    # 裁片信息
    group_id = Column(Integer, nullable=False)  # 组号
    size = Column(String(20), nullable=False)   # 尺码
    image_url = Column(String(500), nullable=True)  # 七牛云 URL
    
    # 尺寸信息
    width_px = Column(Integer, default=0)
    height_px = Column(Integer, default=0)
    width_cm = Column(Float, default=0)
    height_cm = Column(Float, default=0)
    left_cm = Column(Float, default=0)
    top_cm = Column(Float, default=0)
    center_x_cm = Column(Float, default=0)
    center_y_cm = Column(Float, default=0)
    
    # 关联
    record = relationship("PltProcessRecord", back_populates="pieces")


class UserActionLog(Base):
    """用户操作日志表"""
    __tablename__ = "user_action_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    
    # 会话追踪
    session_id = Column(String(64), nullable=True, index=True)
    
    # 操作信息
    action_type = Column(String(50), nullable=False, index=True)  # plt.upload, plt.process, etc.
    action_params = Column(JSON, nullable=True)  # 操作参数
    page = Column(String(50), nullable=True)  # 所在页面
    
    # 结果
    result = Column(String(20), default='success')  # success, error
    error_message = Column(Text, nullable=True)
    
    # 时间
    duration_ms = Column(Integer, nullable=True)  # 操作耗时
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    
    # 关联
    user = relationship("User", backref="action_logs")
