package model

import (
	"time"

	"gorm.io/gorm"
)

type Base struct {
	ID        uint64         `gorm:"primaryKey;autoIncrement" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

type Department struct {
	Base
	Name             string `gorm:"size:255;not null" json:"name"`
	LarkDepartmentID string `gorm:"size:255;uniqueIndex" json:"lark_department_id"`
	ParentID         string `gorm:"size:255" json:"parent_id"`
	LeaderID         string `gorm:"size:255" json:"leader_id"`
	Status           string `gorm:"size:50;default:'active'" json:"status"`
}

type Employee struct {
	Base
	Name            string      `gorm:"size:255;not null" json:"name"`
	Email           string      `gorm:"size:255;index" json:"email"`
	LarkUserID      string      `gorm:"size:255;uniqueIndex" json:"lark_user_id"`
	DepartmentID    string      `gorm:"size:255;index" json:"department_id"` // Primary Lark Department ID
	DepartmentIDs   StringArray `gorm:"type:json" json:"department_ids"`
	DepartmentName  string      `gorm:"-" json:"department_name,omitempty"`
	DepartmentNames StringArray `gorm:"-" json:"department_names,omitempty"`
	Status          string      `gorm:"size:50;default:'active'" json:"status"`
	AvatarURL       string      `gorm:"size:512" json:"avatar_url"`
	JobTitle        string      `gorm:"size:255" json:"job_title"`
}

type OrgSyncRun struct {
	Base
	Provider            string     `gorm:"size:50;not null;default:'lark'" json:"provider"`
	TriggerSource       string     `gorm:"size:32;not null;default:'manual'" json:"trigger_source"`
	Status              string     `gorm:"size:20;not null;default:'running'" json:"status"`
	StartedAt           time.Time  `json:"started_at"`
	FinishedAt          *time.Time `json:"finished_at,omitempty"`
	DurationMs          uint64     `json:"duration_ms"`
	DepartmentsFetched  int        `json:"departments_fetched"`
	DepartmentsUpserted int        `json:"departments_upserted"`
	EmployeesFetched    int        `json:"employees_fetched"`
	EmployeesUpserted   int        `json:"employees_upserted"`
	ErrorCount          int        `json:"error_count"`
	ErrorMessage        string     `gorm:"type:text" json:"error_message,omitempty"`
	StepStatus          JSONB      `gorm:"type:json" json:"step_status,omitempty"`
}
