package collections

import (
	"sync"
	"time"
)

// CollectionItem represents an item required for a collection
type CollectionItem struct {
	ItemID int32 `json:"item_id" db:"item_id"`
	Index  int8  `json:"index" db:"item_index"`
	Found  int8  `json:"found" db:"found"`
}

// CollectionRewardItem represents a reward item for completing a collection
type CollectionRewardItem struct {
	ItemID   int32 `json:"item_id" db:"item_id"`
	Quantity int8  `json:"quantity" db:"quantity"`
}

// Collection represents a collection that players can complete
type Collection struct {
	mu                    sync.RWMutex
	id                    int32
	name                  string
	category              string
	level                 int8
	rewardCoin            int64
	rewardXP              int64
	completed             bool
	saveNeeded            bool
	collectionItems       []CollectionItem
	rewardItems           []CollectionRewardItem
	selectableRewardItems []CollectionRewardItem
	lastModified          time.Time
}

// CollectionData represents collection data for database operations
type CollectionData struct {
	ID       int32  `json:"id" db:"id"`
	Name     string `json:"collection_name" db:"collection_name"`
	Category string `json:"collection_category" db:"collection_category"`
	Level    int8   `json:"level" db:"level"`
}

// CollectionRewardData represents reward data from database
type CollectionRewardData struct {
	CollectionID int32  `json:"collection_id" db:"collection_id"`
	RewardType   string `json:"reward_type" db:"reward_type"`
	RewardValue  string `json:"reward_value" db:"reward_value"`
	Quantity     int8   `json:"reward_quantity" db:"reward_quantity"`
}

// PlayerCollectionData represents player collection progress
type PlayerCollectionData struct {
	CharacterID  int32 `json:"char_id" db:"char_id"`
	CollectionID int32 `json:"collection_id" db:"collection_id"`
	Completed    bool  `json:"completed" db:"completed"`
}

// PlayerCollectionItemData represents player found collection items
type PlayerCollectionItemData struct {
	CharacterID      int32 `json:"char_id" db:"char_id"`
	CollectionID     int32 `json:"collection_id" db:"collection_id"`
	CollectionItemID int32 `json:"collection_item_id" db:"collection_item_id"`
}

// MasterCollectionList manages all available collections in the game
type MasterCollectionList struct {
	mu          sync.RWMutex
	collections map[int32]*Collection
	database    CollectionDatabase
}

// PlayerCollectionList manages collections for a specific player
type PlayerCollectionList struct {
	mu          sync.RWMutex
	characterID int32
	collections map[int32]*Collection
	database    CollectionDatabase
}

// CollectionManager provides high-level collection management
type CollectionManager struct {
	masterList *MasterCollectionList
	database   CollectionDatabase
	itemLookup ItemLookup
}

// CollectionStatistics provides collection system usage statistics
type CollectionStatistics struct {
	TotalCollections       int
	CompletedCollections   int
	ActiveCollections      int
	TotalItems             int
	FoundItems             int
	TotalRewards           int
	PlayersWithCollections int
}

// CollectionInfo provides basic collection information
type CollectionInfo struct {
	ID                int32                  `json:"id"`
	Name              string                 `json:"name"`
	Category          string                 `json:"category"`
	Level             int8                   `json:"level"`
	Completed         bool                   `json:"completed"`
	ReadyToTurnIn     bool                   `json:"ready_to_turn_in"`
	ItemsFound        int                    `json:"items_found"`
	ItemsTotal        int                    `json:"items_total"`
	RewardCoin        int64                  `json:"reward_coin"`
	RewardXP          int64                  `json:"reward_xp"`
	RewardItems       []CollectionRewardItem `json:"reward_items"`
	SelectableRewards []CollectionRewardItem `json:"selectable_rewards"`
	RequiredItems     []CollectionItem       `json:"required_items"`
}

// CollectionProgress represents player progress on a collection
type CollectionProgress struct {
	CollectionID  int32            `json:"collection_id"`
	Name          string           `json:"name"`
	Category      string           `json:"category"`
	Level         int8             `json:"level"`
	Completed     bool             `json:"completed"`
	ReadyToTurnIn bool             `json:"ready_to_turn_in"`
	Progress      float64          `json:"progress_percentage"`
	ItemsFound    []CollectionItem `json:"items_found"`
	ItemsNeeded   []CollectionItem `json:"items_needed"`
	LastUpdated   time.Time        `json:"last_updated"`
}
