package entities

import (
	"database/sql/driver"
	"encoding/json"
	"time"

	"github.com/google/uuid"
)

type JSONB map[string]interface{}

func (j JSONB) Value() (driver.Value, error) {
	return json.Marshal(j)
}

func (j *JSONB) Scan(value interface{}) error {
	if value == nil {
		*j = make(JSONB)
		return nil
	}
	bytes, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(bytes, j)
}

type UserProfile struct {
	UserID            uuid.UUID  `gorm:"type:uuid;primaryKey" json:"user_id"`
	FullName          string     `gorm:"not null;size:150" json:"full_name"`
	DisplayName       *string    `gorm:"size:100" json:"display_name,omitempty"`
	Phone             *string    `gorm:"size:50" json:"phone,omitempty"`
	AvatarURL         *string    `json:"avatar_url,omitempty"`
	JobTitle          *string    `gorm:"size:120" json:"job_title,omitempty"`
	EmployeeNo        *string    `gorm:"size:60" json:"employee_no,omitempty"`
	Bio               *string    `json:"bio,omitempty"`
	Timezone          string     `gorm:"size:64;default:Asia/Jakarta" json:"timezone"`
	Locale            string     `gorm:"size:16;default:id-ID" json:"locale"`
	Preferences       JSONB      `gorm:"type:jsonb;default:'{}'" json:"preferences"`
	NotificationPrefs JSONB      `gorm:"type:jsonb;default:'{}'" json:"notification_prefs"`
	LastSeenAt        *time.Time `json:"last_seen_at,omitempty"`
	CreatedAt         time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt         time.Time  `gorm:"autoUpdateTime" json:"updated_at"`
}

func (UserProfile) TableName() string { return "user_profiles" }
