package v1

import (
	"kami/api/commonApi"
	"kami/internal/consts"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
)

// ListAccountReq 账号列表查询
type ListAccountReq struct {
	g.Meta `path:"/jd-v2/account/list" tags:"JD V2 Account" method:"get" summary:"账号列表"`
	commonApi.CommonPageReq
	Status  consts.CamelOilAccountStatus `json:"status" description:"状态筛选"`
	Keyword string                       `json:"keyword" description:"关键词搜索（账号名称/手机号）"`
}

type AccountListItem struct {
	AccountId       int64                        `json:"accountId" description:"账号ID"`
	AccountName     string                       `json:"accountName" description:"账号名称"`
	Phone           string                       `json:"phone" description:"手机号（脱敏）"`
	Status          consts.CamelOilAccountStatus `json:"status" description:"状态"`
	DailyOrderCount int                          `json:"dailyOrderCount" description:"当日下单数"`
	DailyOrderDate  string                       `json:"dailyOrderDate" description:"当日日期"`
	TotalOrderCount int                          `json:"totalOrderCount" description:"累计下单数"`
	LastUsedAt      *gtime.Time                  `json:"lastUsedAt" description:"最后使用时间"`
	LastLoginAt     *gtime.Time                  `json:"lastLoginAt" description:"最后登录时间"`
	TokenExpireAt   *gtime.Time                  `json:"tokenExpireAt" description:"Token过期时间"`
	RemainingOrders int                          `json:"remainingOrders" description:"剩余可下单数（10-dailyOrderCount）"`
	FailureReason   string                       `json:"failureReason" description:"失败原因"`
	Remark          string                       `json:"remark" description:"备注"`
	CreatedAt       *gtime.Time                  `json:"createdAt" description:"创建时间"`
	UpdatedAt       *gtime.Time                  `json:"updatedAt" description:"更新时间"`
}

type ListAccountRes struct {
	commonApi.CommonPageRes[AccountListItem]
}

// CheckAccountReq 手动检测账号状态
type CheckAccountReq struct {
	g.Meta    `path:"/jd-v2/account/check" tags:"JD V2 Account" method:"post" summary:"棂测账号状态"`
	AccountId int64 `json:"accountId" v:"required#账号ID不能为空" description:"账号ID"`
}

type CheckAccountRes struct {
	IsOnline      bool                         `json:"isOnline" description:"是否在线"`
	Status        consts.CamelOilAccountStatus `json:"status" description:"状态"`
	StatusText    string                       `json:"statusText" description:"状态文本"`
	FailureReason string                       `json:"failureReason" description:"失败原因"`
}

// AccountHistoryReq 账号历史记录查询
type AccountHistoryReq struct {
	g.Meta `path:"/jd-v2/account/history" tags:"JD V2 Account" method:"get" summary:"账号历史记录"`
	commonApi.CommonPageReq
	AccountId int64 `json:"accountId" v:"required#账号ID不能为空" description:"账号ID"`
}

type AccountHistoryItem struct {
	AccountId    int64                            `json:"accountId" description:"账号ID"`
	ChangeType   consts.CamelOilAccountChangeType `json:"changeType" description:"变更类型"`
	ChangeText   string                           `json:"changeText" description:"变更类型文本"`
	StatusBefore consts.CamelOilAccountStatus     `json:"statusBefore" description:"变更前状态"`
	StatusAfter  consts.CamelOilAccountStatus     `json:"statusAfter" description:"变更后状态"`
	FailureCount int                              `json:"failureCount" description:"失败次数"`
	Remark       string                           `json:"remark" description:"备注"`
	CreatedAt    *gtime.Time                      `json:"createdAt" description:"创建时间"`
}

type AccountHistoryRes struct {
	commonApi.CommonPageRes[AccountHistoryItem]
}

// AccountStatisticsReq 账号统计信息
type AccountStatisticsReq struct {
	g.Meta    `path:"/jd-v2/account/statistics" tags:"JD V2 Account" method:"get" summary:"账号统计信息"`
	AccountId int64 `json:"accountId" v:"required#账号ID不能为空" description:"账号ID"`
}

type AccountStatisticsRes struct {
	AccountInfo struct {
		AccountId     int64                        `json:"accountId" description:"账号ID"`
		AccountName   string                       `json:"accountName" description:"账号名称"`
		Phone         string                       `json:"phone" description:"手机号（脱敏）"`
		Status        consts.CamelOilAccountStatus `json:"status" description:"状态"`
		LastUsedAt    *gtime.Time                  `json:"lastUsedAt" description:"最后使用时间"`
		LastLoginAt   *gtime.Time                  `json:"lastLoginAt" description:"最后登录时间"`
		TokenExpireAt *gtime.Time                  `json:"tokenExpireAt" description:"Token过期时间"`
	} `json:"accountInfo" description:"账号基本信息"`

	OrderStats struct {
		TotalOrders     int `json:"totalOrders" description:"总订单数"`
		PaidOrders      int `json:"paidOrders" description:"已支付订单数"`
		PendingOrders   int `json:"pendingOrders" description:"待支付订单数"`
		TimeoutOrders   int `json:"timeoutOrders" description:"超时订单数"`
		DailyOrderCount int `json:"dailyOrderCount" description:"当日下单数"`
		RemainingOrders int `json:"remainingOrders" description:"剩余可下单数"`
	} `json:"orderStats" description:"订单统计"`

	UsageInfo struct {
		OnlineDuration string `json:"onlineDuration" description:"在线时长"`
		LastUsedAt     string `json:"lastUsedAt" description:"最后使用时间"`
		AvgOrdersDaily int    `json:"avgOrdersDaily" description:"日均订单数"`
	} `json:"usageInfo" description:"使用情况"`

	RecentTrend []struct {
		Date       string `json:"date" description:"日期"`
		OrderCount int    `json:"orderCount" description:"订单数"`
	} `json:"recentTrend" description:"近期订单趋势（最近7天）"`
}

// DeleteExpiredAccountsReq 删除所有过期无效账号请求
type DeleteExpiredAccountsReq struct {
	g.Meta `path:"/jd-v2/account/delete-expired" tags:"JD V2 Account" method:"delete" summary:"删除所有过期无效账号"`
}

type DeleteExpiredAccountsRes struct {
	Message      string `json:"message" description:"操作结果信息"`
	DeletedCount int64  `json:"deletedCount" description:"删除的账号数量"`
}
