package models

import (
	"time"
)

// User 通用用户模型
type User struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	UUID      string    `gorm:"type:varchar(64);uniqueIndex" json:"uuid"`
	Name      string    `gorm:"type:varchar(64)" json:"name"`
	Username  string    `gorm:"type:varchar(64);uniqueIndex" json:"username"`
	Phone     string    `gorm:"type:varchar(20);uniqueIndex" json:"phone"`
	Password  string    `gorm:"type:varchar(255)" json:"-"` // 不返回密码
	Role      string    `gorm:"type:varchar(20)" json:"role"` // student, parent, teacher, admin
	Status    int       `gorm:"default:1" json:"status"` // 1:正常, 0:禁用
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// Student 学生模型
type Student struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	StudentNo   string    `gorm:"type:varchar(32);uniqueIndex" json:"student_no"`
	Name        string    `gorm:"type:varchar(64)" json:"name"`
	Gender      int       `gorm:"default:1" json:"gender"` // 1:男, 2:女
	BirthDate   *time.Time `json:"birth_date"`
	ClassID     uint      `json:"class_id"`
	Class       Class     `gorm:"foreignKey:ClassID" json:"class"`
	ParentID    *uint     `json:"parent_id"` // 关联家长
	Status      int       `gorm:"default:1" json:"status"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

// Parent 家长模型
type Parent struct {
	ID       uint      `gorm:"primaryKey" json:"id"`
	Name     string    `gorm:"type:varchar(64)" json:"name"`
	Phone    string    `gorm:"type:varchar(20);uniqueIndex" json:"phone"`
	IDCard   string    `gorm:"type:varchar(32)" json:"id_card"`
	Status   int       `gorm:"default:1" json:"status"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	// 关联的学生
	
}

// Teacher 教师模型
type Teacher struct {
	ID       uint      `gorm:"primaryKey" json:"id"`
	Name     string    `gorm:"type:varchar(64)" json:"name"`
	Phone    string    `gorm:"type:varchar(20);uniqueIndex" json:"phone"`
	SchoolID uint      `json:"school_id"`
	School   School    `gorm:"foreignKey:SchoolID" json:"school"`
	Role     string    `gorm:"type:varchar(32)" json:"role"` // homeroom, school_doctor, sports
	Status   int       `gorm:"default:1" json:"status"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	// 所带班级
	ClassIDs []byte `gorm:"type:json" json:"class_ids"` // JSON格式存储班级ID数组
}

// School 学校模型
type School struct {
	ID       uint      `gorm:"primaryKey" json:"id"`
	Name     string    `gorm:"type:varchar(128)" json:"name"`
	Code     string    `gorm:"type:varchar(32);uniqueIndex" json:"code"`
	Address  string    `gorm:"type:varchar(256)" json:"address"`
	ContactName string `gorm:"type:varchar(64)" json:"contact_name"`
	ContactPhone string `gorm:"type:varchar(20)" json:"contact_phone"`
	Status   int       `gorm:"default:1" json:"status"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	// 关联的班级和教师
	Classes  []Class  `gorm:"foreignKey:SchoolID" json:"classes"`
	Teachers []Teacher `gorm:"foreignKey:SchoolID" json:"teachers"`
}

// Class 班级模型
type Class struct {
	ID       uint      `gorm:"primaryKey" json:"id"`
	Name     string    `gorm:"type:varchar(64)" json:"name"`
	Grade    string    `gorm:"type:varchar(16)" json:"grade"` // 年级
	SchoolID uint      `json:"school_id"`
	School   School    `gorm:"foreignKey:SchoolID" json:"school"`
	TeacherID *uint    `json:"teacher_id"` // 班主任
	Teacher  *Teacher  `gorm:"foreignKey:TeacherID" json:"teacher"`
	StudentCount int   `gorm:"default:0" json:"student_count"`
	Status   int       `gorm:"default:1" json:"status"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`

	// 关联的学生
	Students []Student `gorm:"foreignKey:ClassID" json:"students"`
}

// ParentStudentRel 家长-学生关联表
type ParentStudentRel struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	ParentID   uint      `json:"parent_id"`
	StudentID  uint      `json:"student_id"`
	Relation   string    `gorm:"type:varchar(16)" json:"relation"` // father, mother, other
	IsPrimary  bool      `gorm:"default:false" json:"is_primary"` // 是否主要监护人
	CreatedAt  time.Time `json:"created_at"`

	
	
}

// UserAccount 用户账号模型（统一账号体系）
type UserAccount struct {
	ID            uint      `gorm:"primaryKey" json:"id"`
	Username      string    `gorm:"type:varchar(64);uniqueIndex" json:"username"`
	PasswordHash  string    `gorm:"type:varchar(128)" json:"-"`
	Phone         string    `gorm:"type:varchar(20);uniqueIndex" json:"phone"`
	UserType      string    `gorm:"type:varchar(16)" json:"user_type"` // student, parent, teacher, admin
	UserID        uint      `json:"user_id"` // 关联的具体用户ID
	Status        int       `gorm:"default:1" json:"status"`
	LastLoginAt   *time.Time `json:"last_login_at"`
	LastLoginIP   string    `gorm:"type:varchar(45)" json:"last_login_ip"`
	CreatedAt     time.Time `json:"created_at"`
	UpdatedAt     time.Time `json:"updated_at"`
}