package pickup

import (
	"errors"
	"time"

	"gorm.io/datatypes"
)

// 提号状态
const (
	StatusPickingUp = "picking_up"
	StatusCompleted = "completed"
	StatusCancelled = "cancelled"
)

var (
	ErrDependencyUnavailable = errors.New("dependency unavailable")
	ErrListingUnavailable    = errors.New("listing unavailable")
	ErrPickupNotFound        = errors.New("pickup not found")
	ErrPickupNotPickingUp    = errors.New("pickup not in picking_up status")
	ErrInvalidAmount         = errors.New("invalid amount")
	ErrInvalidProfit         = errors.New("invalid profit amount")
	ErrDuplicatePickup       = errors.New("duplicate pickup in progress")
)

type PickupDTO struct {
	ID                uint64         `json:"id"`
	PickupNo          string         `json:"pickup_no"`
	ListingID         uint64         `json:"listing_id"`
	ListingNo         string         `json:"listing_no"`
	AccountID         uint64         `json:"account_id"`
	AccountTitle      string         `json:"account_title"`
	ServerRegion      string         `json:"server_region"`
	LoginPlatform     string         `json:"login_platform"`
	OwnerID           uint64         `json:"owner_id"`
	OwnerPhone        string         `json:"owner_phone"`
	AdminID           uint64         `json:"admin_id"`
	Platform          string         `json:"platform"`
	ListingPriceCent  int64          `json:"listing_price_cent"`
	OwnerPriceCent    int64          `json:"owner_price_cent"`
	WebsiteProfitCent int64          `json:"website_profit_cent"`
	ProfitAmountCent  int64          `json:"profit_amount_cent"`
	SellerRatio       float64        `json:"seller_ratio"`
	BuyerRatio        float64        `json:"buyer_ratio"`
	AccountSnapshot   datatypes.JSON `json:"account_snapshot,omitempty"`
	SettleAmountCent  int64          `json:"settle_amount_cent"`
	Status            string         `json:"status"`
	Remark            string         `json:"remark"`
	CompleteRemark    string         `json:"complete_remark"`
	CreatedAt         time.Time      `json:"created_at"`
	CompletedAt       *time.Time     `json:"completed_at,omitempty"`
	CancelledAt       *time.Time     `json:"cancelled_at,omitempty"`
}

type CreateRequest struct {
	ListingID        uint64 `json:"listing_id" binding:"required"`
	Platform         string `json:"platform"`
	ProfitAmountCent int64  `json:"profit_amount_cent"`
	Remark           string `json:"remark"`
}

type CompleteRequest struct {
	SettleAmountCent int64  `json:"settle_amount_cent" binding:"required,min=1"`
	ProfitAmountCent *int64 `json:"profit_amount_cent"`
	CompleteRemark   string `json:"complete_remark"`
}

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

type AdminPickupQuery struct {
	Page     int
	PageSize int
	Keyword  string
	Status   string
}

type SellerPickupQuery struct {
	Page     int
	PageSize int
	Status   string
}

type AvailableListingDTO struct {
	ID                      uint64  `json:"id"`
	ListingNo               string  `json:"listing_no"`
	AccountID               uint64  `json:"account_id"`
	AccountTitle            string  `json:"account_title"`
	ServerRegion            string  `json:"server_region"`
	LoginPlatform           string  `json:"login_platform"`
	OwnerID                 uint64  `json:"owner_id"`
	OwnerPhone              string  `json:"owner_phone"`
	ListingPriceCent        int64   `json:"listing_price_cent"`
	OwnerPriceCent          int64   `json:"owner_price_cent"`
	OwnerPurePriceCent      int64   `json:"owner_pure_price_cent"`
	OwnerExtraItemPriceCent int64   `json:"owner_extra_item_price_cent"`
	OwnerTotalPriceCent     int64   `json:"owner_total_price_cent"`
	WebsiteProfitCent       int64   `json:"website_profit_cent"`
	SellerRatio             float64 `json:"seller_ratio"`
	BuyerRatio              float64 `json:"buyer_ratio"`
}

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

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