package listing

import (
	"encoding/json"
	"strings"
	"time"

	"hfb_sys/backend/internal/auditlog"
)

type ListingDTO struct {
	ID             uint64         `json:"id"`
	AccountID      uint64         `json:"account_id"`
	OwnerID        uint64         `json:"owner_id"`
	OwnerPhone     string         `json:"owner_phone,omitempty"`
	OwnerNickname  string         `json:"owner_nickname,omitempty"`
	Title          string         `json:"title"`
	Description    string         `json:"description"`
	GameName       string         `json:"game_name"`
	ServerRegion   string         `json:"server_region"`
	LoginPlatform  string         `json:"login_platform"`
	RankLevel      string         `json:"rank_level"`
	HafCoinAmount  int64          `json:"haf_coin_amount"`
	AssetSummary   map[string]any `json:"asset_summary,omitempty"`
	ScreenshotURLS []string       `json:"screenshot_urls"`
	CoverURL       string         `json:"cover_url"`
	Price          float64        `json:"price"`
	DepositAmount  float64        `json:"deposit_amount"`
	IsAccelerated  bool           `json:"is_accelerated_sale"`
	InTransaction  bool           `json:"in_transaction"`
	Status         string         `json:"status"`
	ReviewStatus   string         `json:"review_status"`
	ReviewReason   string         `json:"review_reason"`
	PublishedAt    *time.Time     `json:"published_at"`
	CreatedAt      time.Time      `json:"created_at"`
	UpdatedAt      time.Time      `json:"updated_at"`
}

type CreateRequest struct {
	Title          string         `json:"title" binding:"required"`
	Description    string         `json:"description"`
	ServerRegion   string         `json:"server_region" binding:"required"`
	LoginPlatform  string         `json:"login_platform"`
	RankLevel      string         `json:"rank_level"`
	HafCoinAmount  int64          `json:"haf_coin_amount"`
	AssetSummary   map[string]any `json:"asset_summary"`
	ScreenshotURLS []string       `json:"screenshot_urls"`
	Price          float64        `json:"price"`
	DepositAmount  float64        `json:"deposit_amount"`
}

type UpdateRequest = CreateRequest

type ReviewRequest struct {
	Reason string `json:"reason"`
}

type AdminPriceAdjustRequest struct {
	BuyerRatio      float64 `json:"buyer_ratio"`
	BuyerTotalPrice float64 `json:"buyer_total_price"`
	Reason          string  `json:"reason"`
}

type AdminListQuery struct {
	OwnerID      uint64
	Status       string
	ReviewStatus string
	Limit        int
	Page         int
	PageSize     int
}

type AdminListResult struct {
	Items    []ListingDTO `json:"items"`
	Total    int64        `json:"total"`
	Page     int          `json:"page"`
	PageSize int          `json:"page_size"`
}

type PublicListQuery struct {
	Page           int
	PageSize       int
	Keyword        string
	Sort           string
	Zone           string
	Server         []string
	Region         []string
	LoginMethod    []string
	Rank           []string
	Insurance      []string
	Stamina        []string
	Load           []string
	SkinGroup      []string
	SkinName       []string
	MinCoin        *float64
	MaxCoin        *float64
	MinPrice       *float64
	MaxPrice       *float64
	MinDeposit     *float64
	MaxDeposit     *float64
	MinTotal       *float64
	MaxTotal       *float64
	MinFireLevel   *float64
	MaxFireLevel   *float64
	MinSecretKD    *float64
	MaxSecretKD    *float64
	ResourceRanges map[string]NumberRange
}

type NumberRange struct {
	Min *float64
	Max *float64
}

type PublicListResult struct {
	Items      []ListingDTO     `json:"items"`
	Total      int64            `json:"total"`
	Page       int              `json:"page"`
	PageSize   int              `json:"page_size"`
	ZoneCounts map[string]int64 `json:"zone_counts"`
}

type AdminActionRequest struct {
	Reason string `json:"reason" binding:"required"`
}

type AuditMeta = auditlog.Meta

type ExternalUploadRequest struct {
	UploadTime   int64           `json:"uploadTime"`
	UploaderName string          `json:"uploaderName"`
	Uploaderame  string          `json:"uploaderame"`
	Data         json.RawMessage `json:"data"`
}

func (r ExternalUploadRequest) normalizedUploaderName() string {
	if name := strings.TrimSpace(r.UploaderName); name != "" {
		return name
	}
	return strings.TrimSpace(r.Uploaderame)
}

type ExternalAccountData struct {
	LoginMethod      string                   `json:"loginMethod"`
	Rank             string                   `json:"rank"`
	Level            int                      `json:"level"`
	SafeSlots        int                      `json:"safeSlots"`
	SecretKD         float64                  `json:"secretKD"`
	DailyLossM       float64                  `json:"dailyLossM"`
	Deposit          float64                  `json:"deposit"`
	BanRecord        string                   `json:"banRecord"`
	CommonRegion     string                   `json:"commonRegion"`
	ContactPhone     string                   `json:"contactPhone"`
	OwnerOnlineTime  string                   `json:"ownerOnlineTime"`
	Currency         ExternalUploadCurrency   `json:"currency"`
	DailyConsumption ExternalDailyConsumption `json:"dailyConsumption"`
	Inventory        ExternalUploadInventory  `json:"inventory"`
}

type ExternalUploadCurrency struct {
	HafuCoin     float64 `json:"hafuCoin"`
	RecycleRatio float64 `json:"recycleRatio"`
	RecycleRent  float64 `json:"recycleRent"`
}

type ExternalDailyConsumption struct {
	Stamina int `json:"stamina"`
	Weight  int `json:"weight"`
}

type ExternalUploadInventory struct {
	AWMBullets    int      `json:"awmBullets"`
	Level6Helmets int      `json:"level6Helmets"`
	Level6Armor   int      `json:"level6Armor"`
	Skins         []string `json:"skins"`
}

type ExternalUploadMeta struct {
	IP         string
	UserAgent  string
	RawPayload []byte
}

type ExternalUploadResult struct {
	Index        int    `json:"index"`
	ListingID    uint64 `json:"listing_id,omitempty"`
	AccountID    uint64 `json:"account_id,omitempty"`
	Status       string `json:"status,omitempty"`
	ReviewStatus string `json:"review_status,omitempty"`
	Error        string `json:"error,omitempty"`
}

type ExternalUploadResponse struct {
	ListingID    uint64                 `json:"listing_id,omitempty"`
	AccountID    uint64                 `json:"account_id,omitempty"`
	Status       string                 `json:"status,omitempty"`
	ReviewStatus string                 `json:"review_status,omitempty"`
	Total        int                    `json:"total"`
	Success      int                    `json:"success"`
	Failed       int                    `json:"failed"`
	Items        []ExternalUploadResult `json:"items,omitempty"`
}
