package domain

import (
	"context"
	"fmt"
	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)

type User struct {
	Id           int64   `json:"id,omitempty"`           // 用户ID
	CompanyId    int64   `json:"companyId,omitempty"`    // 公司ID
	DepartmentId int64   `json:"departmentId,omitempty"` // 部门ID
	Roles        []int64 `json:"roleId,omitempty"`       // 角色
	Flag         int     `json:"flag,omitempty"`         // 标识 1:管理员 2:普通用户 (有绑定角色是管理员)
	Name         string  `json:"name,omitempty"`         // 名称
	Avatar       string  `json:"avatar,omitempty"`       // 头像
	Phone        string  `json:"phone,omitempty"`        // 手机号 唯一
	Position     string  `json:"position,omitempty"`     // 职位
	Enable       int     `json:"enable,omitempty"`       // 启用状态 1:启用 2:禁用
	AuditStatus  int     `json:"auditStatus,omitempty"`  // 审核状态 0:待审核 1:审核通过 2:拒绝
	Follower     []int64 `json:"followers,omitempty"`    // 关注我的人 (冗余)
	Following    []int64 `json:"following,omitempty"`    // 我关注的人 (冗余)

	CreatedAt int64 `json:"createdAt,omitempty"`
	UpdatedAt int64 `json:"updatedAt,omitempty"`
	DeletedAt int64 `json:"deletedAt,omitempty"`
	Version   int   `json:"version,omitempty"`
}

type UserRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
	Update(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*User, error)
	FindOneByCompanyIdAndPhone(ctx context.Context, conn transaction.Conn, companyId int64, phone string, status []int) (*User, error)
	Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*User, error)
}

func (m *User) Identify() interface{} {
	if m.Id == 0 {
		return nil
	}
	return m.Id
}

const (
	UserAuditStatusWait   = 0 // 审核中
	UserAuditStatusPassed = 1 // 审核通过
	UserAuditStatusReject = 2 // 审核拒绝
)

const (
	LoginTypeWechatLogin        string = "wechat-login"         // 微信登录
	LoginTypeWechatPhoneLogin   string = "wechat-phone-login"   // 微信手机号登录
	LoginTypePhonePasswordLogin string = "phone-password-login" // 手机密码登录
	LoginTypePhoneSmsCodeLogin  string = "phone-sms-code-login" // 手机验证码登录
)

const (
	UserEnable  = 1
	UserDisable = 2
)

func (m *User) Audit(status int) error {
	if !lo.Contains([]int{UserAuditStatusWait, UserAuditStatusPassed, UserAuditStatusReject}, status) {
		return fmt.Errorf("unknown status:%d", status)
	}
	if m.AuditStatus != UserAuditStatusWait {
		return fmt.Errorf("用户不是在待审核状态")
	}
	m.AuditStatus = status
	return nil
}

type (
	LoginCreator interface {
		WechatLogin(r WechatLoginRequest) (*LoginInfo, error)
		PhonePasswordLogin(phone string, password string) (*LoginInfo, error)
		PhoneSmsCodeLogin(phone string, code string) (*LoginInfo, error)
		WechatPhoneLogin(r WechatLoginRequest) (*LoginInfo, error)
	}
	WechatLoginRequest struct {
		Code          string // 授权码
		EncryptedData string // 包括敏感数据在内的完整用户信息的加密数据
		IV            string // 加密算法的初始向量
	}
	LoginInfo struct {
		User    *User
		Phone   string
		Message string
	}
)