/**
 * 自然写互动课堂教学管理云平台软件 V1.0
 * 
 * 数据模型 - 设备实体 / 作业实体 / 笔迹数据实体
 * 设备表(device)：MySQL
 * 作业表(assignment)：MySQL
 * 笔迹数据(stroke_data)：MongoDB
 */
package com.writech.cloud.model;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.*;

// ==================== 设备实体 ====================

/**
 * 设备注册表实体（MySQL）
 * 管理点阵笔、网关、终端设备、算力盒
 */
@Entity
@Table(name = "device", indexes = {
    @Index(name = "idx_mac", columnList = "macAddr", unique = true),
    @Index(name = "idx_school_type", columnList = "schoolId, type"),
    @Index(name = "idx_classroom", columnList = "classroomId")
})
class Device {

    @Id
    @Column(length = 32)
    private String id;

    /** 设备类型：pen/gateway/terminal/edge_box */
    @Column(nullable = false, length = 16)
    private String type;

    /** 设备MAC地址（全局唯一） */
    @Column(nullable = false, length = 17, unique = true)
    private String macAddr;

    /** 设备序列号 */
    @Column(length = 32)
    private String serialNumber;

    /** 固件版本号 */
    @Column(length = 16)
    private String firmwareVersion;

    /** 绑定用户ID */
    @Column(length = 32)
    private String bindUserId;

    /** 所属学校ID */
    @Column(length = 32)
    private String schoolId;

    /** 所属教室ID */
    @Column(length = 32)
    private String classroomId;

    /** 设备状态：1=在线, 0=离线, -1=故障 */
    @Column(nullable = false)
    private int status = 0;

    /** 电池电量百分比（0-100，仅笔设备） */
    private Integer batteryLevel;

    /** 当前连接的笔数量（仅网关设备） */
    private Integer connectedPenCount;

    /** CPU使用率（仅网关/算力盒） */
    private Double cpuUsage;

    /** 内存使用率（仅网关/算力盒） */
    private Double memoryUsage;

    /** 注册时间 */
    @Column(nullable = false)
    private LocalDateTime registerTime;

    /** 最后心跳时间 */
    private LocalDateTime lastHeartbeat;

    // Getter/Setter
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getMacAddr() { return macAddr; }
    public void setMacAddr(String macAddr) { this.macAddr = macAddr; }
    public String getSerialNumber() { return serialNumber; }
    public void setSerialNumber(String sn) { this.serialNumber = sn; }
    public String getFirmwareVersion() { return firmwareVersion; }
    public void setFirmwareVersion(String v) { this.firmwareVersion = v; }
    public String getBindUserId() { return bindUserId; }
    public void setBindUserId(String id) { this.bindUserId = id; }
    public String getSchoolId() { return schoolId; }
    public void setSchoolId(String id) { this.schoolId = id; }
    public String getClassroomId() { return classroomId; }
    public void setClassroomId(String id) { this.classroomId = id; }
    public int getStatus() { return status; }
    public void setStatus(int s) { this.status = s; }
    public Integer getBatteryLevel() { return batteryLevel; }
    public void setBatteryLevel(Integer l) { this.batteryLevel = l; }
    public Integer getConnectedPenCount() { return connectedPenCount; }
    public void setConnectedPenCount(Integer c) { this.connectedPenCount = c; }
    public Double getCpuUsage() { return cpuUsage; }
    public void setCpuUsage(Double u) { this.cpuUsage = u; }
    public Double getMemoryUsage() { return memoryUsage; }
    public void setMemoryUsage(Double u) { this.memoryUsage = u; }
    public LocalDateTime getRegisterTime() { return registerTime; }
    public void setRegisterTime(LocalDateTime t) { this.registerTime = t; }
    public LocalDateTime getLastHeartbeat() { return lastHeartbeat; }
    public void setLastHeartbeat(LocalDateTime t) { this.lastHeartbeat = t; }
}

// ==================== 作业实体 ====================

/**
 * 作业/试卷发布表实体（MySQL）
 */
@Entity
@Table(name = "assignment", indexes = {
    @Index(name = "idx_class_status", columnList = "classId, status"),
    @Index(name = "idx_teacher", columnList = "teacherId")
})
class Assignment {

    @Id
    @Column(length = 32)
    private String id;

    /** 发布教师ID */
    @Column(nullable = false, length = 32)
    private String teacherId;

    /** 班级ID */
    @Column(nullable = false, length = 32)
    private String classId;

    /** 作业标题 */
    @Column(nullable = false, length = 128)
    private String title;

    /** 类型：homework(作业)/exam(考试)/practice(练习) */
    @Column(nullable = false, length = 16)
    private String type;

    /** 学科 */
    @Column(length = 32)
    private String subject;

    /** 截止时间 */
    private LocalDateTime deadline;

    /** 状态：draft/published/closed/graded */
    @Column(nullable = false, length = 16)
    private String status;

    /** 发布时间 */
    private LocalDateTime publishTime;

    /** 满分值 */
    private double totalScore;

    /** 题目总数 */
    private int questionCount;

    /** 关联的点阵码页面ID列表（JSON数组） */
    @Column(columnDefinition = "TEXT")
    private String dotCodePagesJson;

    @Transient
    private List<String> dotCodePages;

    // Getter/Setter
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getTeacherId() { return teacherId; }
    public void setTeacherId(String id) { this.teacherId = id; }
    public String getClassId() { return classId; }
    public void setClassId(String id) { this.classId = id; }
    public String getTitle() { return title; }
    public void setTitle(String t) { this.title = t; }
    public String getType() { return type; }
    public void setType(String t) { this.type = t; }
    public String getSubject() { return subject; }
    public void setSubject(String s) { this.subject = s; }
    public LocalDateTime getDeadline() { return deadline; }
    public void setDeadline(LocalDateTime d) { this.deadline = d; }
    public String getStatus() { return status; }
    public void setStatus(String s) { this.status = s; }
    public LocalDateTime getPublishTime() { return publishTime; }
    public void setPublishTime(LocalDateTime t) { this.publishTime = t; }
    public double getTotalScore() { return totalScore; }
    public void setTotalScore(double s) { this.totalScore = s; }
    public int getQuestionCount() { return questionCount; }
    public void setQuestionCount(int c) { this.questionCount = c; }
    public List<String> getDotCodePages() { return dotCodePages; }
    public void setDotCodePages(List<String> p) { this.dotCodePages = p; }
}

// ==================== 笔迹数据实体 ====================

/**
 * 笔迹原始数据实体（MongoDB）
 * 
 * JSON文档结构：
 * {
 *   student_id: "...",
 *   assignment_id: "...",
 *   pen_id: "...",
 *   page_id: "...",
 *   strokes: [{x, y, pressure, timestamp, penUp}, ...],
 *   createTime: "...",
 *   processingStatus: "received/processing/completed/failed"
 * }
 */
class StrokeData {

    private String id;
    private String studentId;
    private String assignmentId;
    private String penId;
    private String pageId;
    private List<Map<String, Object>> strokes;
    private LocalDateTime createTime;
    private LocalDateTime processedTime;
    private String processingStatus;  // received/processing/completed/failed

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getStudentId() { return studentId; }
    public void setStudentId(String id) { this.studentId = id; }
    public String getAssignmentId() { return assignmentId; }
    public void setAssignmentId(String id) { this.assignmentId = id; }
    public String getPenId() { return penId; }
    public void setPenId(String id) { this.penId = id; }
    public String getPageId() { return pageId; }
    public void setPageId(String id) { this.pageId = id; }
    public List<Map<String, Object>> getStrokes() { return strokes; }
    public void setStrokes(List<Map<String, Object>> s) { this.strokes = s; }
    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime t) { this.createTime = t; }
    public LocalDateTime getProcessedTime() { return processedTime; }
    public void setProcessedTime(LocalDateTime t) { this.processedTime = t; }
    public String getProcessingStatus() { return processingStatus; }
    public void setProcessingStatus(String s) { this.processingStatus = s; }
}
