package service

import (
	"context"
	"youngee_b_api/db"
	"youngee_b_api/model/http_model"
)

var User *user

type user struct {
	Phone        string
	User         string // 账号
	Username     string // 后台用户名
	Email        string // 电子邮件
	EnterpriseID int64  //企业ID
}

func (*user) Update(ctx context.Context, newUser http_model.UpdateUserInfoRequest, ID int64, enterpriseID string) error {
	_, err := db.UpdateUser(ctx, ID, newUser.Username, newUser.Email)
	if err != nil {
		return err
	}
	err = db.UpdateEnterprise(ctx, enterpriseID, newUser.BusinessName)
	if err != nil {
		return err
	}
	return nil
}

func (*user) Find(ctx context.Context, EnterpriseID string, ID int64) (*http_model.FindUserInfoData, error) {
	enterprise, err := db.GetEnterpriseByEnterpriseID(ctx, EnterpriseID)
	if err != nil {
		return nil, err
	}
	user, err := db.GetUserByID(ctx, ID)
	if err != nil {
		return nil, err
	}
	UserInfo := http_model.FindUserInfoData{
		ID:           enterprise.EnterpriseID,
		Username:     user.Username,
		Email:        user.Email,
		Phone:        user.Phone,
		BusinessName: enterprise.BusinessName,
	}
	return &UserInfo, nil
}
