package services

import (
	"go-account-register/models"
	"go-account-register/utils"
	"time"

	"gorm.io/gorm"
)

type TwitterAccountService struct {
	db *gorm.DB
}

func InitTwitterAccountService() *TwitterAccountService {
	db := utils.GetDB()
	return &TwitterAccountService{db: db}
}

func (t *TwitterAccountService) Create(params *models.TwitterAccount) int64 {
	params.CreateTime = time.Now().Unix()
	params.UpdateTime = time.Now().Unix()
	result := t.db.Create(params)
	if result.Error != nil {
		return 0
	}
	return params.ID
}

func (t *TwitterAccountService) GetInfo(id int64) *models.TwitterAccount {
	var account models.TwitterAccount
	result := t.db.Model(&models.TwitterAccount{}).Where("id = ?", id).First(&account)
	if result.Error != nil {
		return nil
	}
	return &account
}

// 更改账号状态
func (u *TwitterAccountService) ChangeAccountStatus(accountId int, status string) bool {
	u.db.Model(&models.TwitterAccount{}).Where("id = ?", accountId).Updates(map[string]interface{}{"status": status, "update_time": time.Now().Unix()})
	return true
}

// 更改账号登录状态
func (u *TwitterAccountService) ChangeAccountLoginStatus(accountId int, loginStatus string) bool {
	u.db.Model(&models.TwitterAccount{}).Where("id = ?", accountId).Updates(map[string]interface{}{"login_status": loginStatus, "update_time": time.Now().Unix()})
	return true
}

// 下线所有账号
func (u *TwitterAccountService) OfflineAllAccount() bool {
	u.db.Model(&models.TwitterAccount{}).Where("login_status = ?", "登录成功").Updates(&models.TwitterAccount{
		LoginStatus: "账号下线",
		UpdateTime:  time.Now().Unix(),
	})
	return true
}

// 获取可用账号
func (u *TwitterAccountService) GetAvailableAccount(params *models.TwitterAccount) []models.TwitterAccount {
	var users []models.TwitterAccount
	res := u.db.Model(&models.TwitterAccount{}).Where(`type = ? AND (login_status != "账号异常" OR login_status IS NULL)`, params.Type)
	// if params.Username != "" {
	// 	res.Where("username LIKE ?", "%"+params.Username+"%")
	// }
	res.Order("id DESC").Find(&users)

	return users
}

// 获取ID范围内的账号
func (u *TwitterAccountService) GetAccountListByIdRange(minId, maxId int64) []models.TwitterAccount {
	var users []models.TwitterAccount
	res := u.db.Model(&models.TwitterAccount{}).Where(`id >= ? AND id <= ?`, minId, maxId)
	res.Order("id DESC").Find(&users)
	return users
}

// 获取未运行或者运行失败的账号
func (u *TwitterAccountService) GetAccountListByStatus(params *models.TwitterAccount) []models.TwitterAccount {
	var users []models.TwitterAccount
	res := u.db.Model(&models.TwitterAccount{}).Where(`type = ? AND (login_status != "账号异常" OR login_status IS NULL) AND (status != ? OR status IS NULL)`, params.Type, params.Status)
	// if params.Username != "" {
	// 	res.Where("username LIKE ?", "%"+params.Username+"%")
	// }
	res.Order("id DESC").Find(&users)
	return users
}

// 清空状态字段
func (u *TwitterAccountService) ClearAccountStatus(params *models.TwitterAccount) bool {
	u.db.Model(&models.TwitterAccount{}).Where("type = ?", params.Type).Update("status", nil)
	return true
}

// 清空状态字段
func (u *TwitterAccountService) ChangeAccountStatusByStatus(params *models.TwitterAccount, status string) bool {
	u.db.Model(&models.TwitterAccount{}).Where("status = ?", params.Status).Update("status", status)
	return true
}

func (u *TwitterAccountService) Update(id int64, params *models.TwitterAccount) {
	params.UpdateTime = time.Now().Unix()
	u.db.Model(&models.TwitterAccount{}).Where("id = ?", id).Updates(params)
}

func (u *TwitterAccountService) Delete(id int64) {
	u.db.Delete(&models.TwitterAccount{}, id)
}

// 获取可用账号
func (u *TwitterAccountService) GetAvailableImportAccount(params *models.TwitterAccount) []models.TwitterAccount {
	var users []models.TwitterAccount
	res := u.db.Model(&models.TwitterAccount{}).Where(`(browser_id = '' OR browser_id IS NULL) AND (login_status != "账号异常" OR login_status IS NULL)`).Where(params)
	// if params.Username != "" {
	// 	res.Where("username LIKE ?", "%"+params.Username+"%")
	// }
	res.Order("id DESC").Find(&users)

	return users
}
