package schema

import (
	"github.guxuan/haibei/pkg/util"
)

type Customer struct {
	util.BaseModel
	Name      string `json:"name" gorm:"size:128;index;comment:姓名"`
	Phone     string `json:"phone" gorm:"size:128;index;comment:手机号"`
	InviterID uint   `json:"inviterId" gorm:"index;comment:邀请人id"`
	Birthday  string `json:"birthday" gorm:"size:128;index;comment:生日"`
	CompanyID uint   `json:"companyId" gorm:"index;comment:绑定社区id"`
	AreaID    uint   `json:"areaId" gorm:"index;comment:城市id"`
	WxSign    string `json:"wxSign" gorm:"size:1024;comment:微信签名token"`
	Avatar    string `json:"avatar" gorm:"size:1024;comment:头像"`
	Grade     int    `json:"grade" gorm:"not null;default:0;comment:会员等级"`
	Balance   int    `json:"balance" gorm:"not null;default:0;comment:积分余额"`
	MentorID  string `json:"mentorId" gorm:"index;comment:顾问id"`
	Status    string `json:"status" gorm:"size:20;index;comment:状态"`
}

type CustomerQueryParam struct {
	util.PaginationParam
	LikeName  string `form:"name" `
	LikePhone string `form:"phone" `
	CompanyID uint   `form:"companyId" `
	Grade     int    `form:"grade" `
	InviterId uint   `form:"inviterId"`

	Status string `form:"status" `
	AreaID uint   `form:"areaId"`
}

type CustomerQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Customer` struct.
type CustomerQueryResult struct {
	Data       Customers
	PageResult *util.PaginationResult
}

// Defining the slice of `Customer` struct.
type Customers []*Customer

// Defining the data structure for creating a `Customer` struct.
type CustomerForm struct {
	Name      string `json:"name" `
	Phone     string `json:"phone" `
	Birthday  string `json:"birthday" `
	CompanyID uint   `json:"companyId" `
	WxSign    string `json:"wxSign" `
	AreaID    uint   `json:"areaId"`
	InviterID uint   `json:"inviterId"`
	Avatar    string `json:"avatar" `
	Grade     int    `json:"grade" `
	Balance   int    `json:"balance" `
	Status    string `json:"status" `
}

type CustomerLoginForm struct {
	WxSign string `json:"wxSign" binding:"required"`
}

type CustomerBindPhoneForm struct {
	Phone  string  `json:"phone" binding:"required"`
	Code   string  `json:"code" binding:"required"`
	Avatar string  `json:"avatar" binding:"required"`
	Lat    float64 `json:"lat" binding:"required"`
	Lon    float64 `json:"lon" binding:"required"`
}

type CustomerLoginToken struct {
	AccessToken string `json:"access_token"` // Access token (JWT)
	Phone       string `json:"phone"`
	TokenType   string `json:"token_type"` // Token type (Usage: Authorization=${token_type} ${access_token})
	ExpiresAt   int64  `json:"expires_at"` // Expired time (Unit: second)
}

// A validation function for the `CustomerForm` struct.
func (a *CustomerForm) Validate() error {
	return nil
}

// Convert `CustomerForm` to `Customer` object.
func (a *CustomerForm) FillTo(customer *Customer) error {
	customer.Name = a.Name
	customer.Phone = a.Phone
	customer.Birthday = a.Birthday
	customer.CompanyID = a.CompanyID
	customer.WxSign = a.WxSign
	customer.AreaID = a.AreaID
	customer.Avatar = a.Avatar
	customer.Grade = a.Grade
	customer.InviterID = a.InviterID
	customer.Balance = a.Balance
	if a.Status != "" {
		customer.Status = a.Status
	} else {
		customer.Status = "enabled"
	}
	return nil
}
