package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"

	humanize "github.com/dustin/go-humanize"
)

// fetchCards fetches cards from the API using the provided limit, cursor, and set type.
func fetchCards(limit, cursor int, setType string) ([]*Card, error) {
	// Define the input data structure
	input := Input{
		Part0: struct {
			JSON JSONPayload `json:"json"`
		}{
			JSON: JSONPayload{
				Query:           "",
				Sort:            "relevance",
				Set:             setType,
				Filters:         []interface{}{},
				Limit:           limit,
				VariantLimit:    false,
				CollectionLimit: false,
				Cursor:          cursor,
				Direction:       "forward",
			},
		},
	}

	// Serialize the input data structure to JSON
	jsonData, err := json.Marshal(input)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal input data: %w", err)
	}

	// URL encode the JSON data
	urlEncodedInput := url.QueryEscape(string(jsonData))

	// Construct the full URL
	apiURL := fmt.Sprintf("https://curiosa.io/api/trpc/card.search?batch=1&input=%s", urlEncodedInput)

	// Create a new HTTP request
	req, err := http.NewRequest("GET", apiURL, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}

	// Set essential headers
	setHeaders(req)

	// Perform the HTTP request
	start := time.Now()
	client := &http.Client{}
	resp, err := client.Do(req)
	duration := time.Since(start)
	if err != nil {
		return nil, fmt.Errorf("failed to perform request: %w", err)
	}
	defer resp.Body.Close()

	// Check for non-200 status codes
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("non-200 response: %s", resp.Status)
	}

	// Read and decode the JSON response
	var result []struct {
		Result struct {
			Data struct {
				JSON struct {
					Cards []*Card `json:"cards"`
				} `json:"json"`
			} `json:"data"`
		} `json:"result"`
	}
	err = json.NewDecoder(resp.Body).Decode(&result)
	if err != nil {
		return nil, fmt.Errorf("failed to decode response body: %w", err)
	}

	// Extract cards from the result
	var cards []*Card
	if len(result) > 0 {
		cards = result[0].Result.Data.JSON.Cards
	}

	fmt.Printf("Cursor: %d, Limit: %d, Duration: %s, Status: %d, Cards: %d\n",
		cursor, limit, duration, resp.StatusCode, len(cards))

	return cards, nil
}

// fetchAllCards fetches all cards by making multiple requests with different cursor values.
func fetchAllCards(limit int, setType string) ([]*Card, error) {
	var allCards []*Card
	cursor := 0

	for {
		cards, err := fetchCards(limit, cursor, setType)
		if err != nil {
			return nil, err
		}

		allCards = append(allCards, cards...)

		// If the number of returned cards is less than the limit, we've fetched all cards.
		if len(cards) < limit {
			break
		}

		// Update cursor for the next batch
		cursor += limit
	}

	return allCards, nil
}

// downloadImage downloads an image from the given URL and saves it to the specified path.
func downloadImage(url, filepath, setType string) error {
	if _, err := os.Stat(filepath); err == nil {
		fmt.Printf("File already exists: %s\n", filepath)
		return nil
	}

	start := time.Now()
	resp, err := http.Get(url)
	duration := time.Since(start)
	if err != nil {
		return fmt.Errorf("failed to fetch image: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("non-200 response: %s", resp.Status)
	}

	file, err := os.Create(filepath)
	if err != nil {
		return fmt.Errorf("failed to create image file: %w", err)
	}
	defer file.Close()

	bodySize, err := io.Copy(file, resp.Body)
	if err != nil {
		return fmt.Errorf("failed to save image: %w", err)
	}

	fmt.Printf("Downloaded %s | Set: %s | Status: %d | Size: %s | Duration: %s\n",
		filepath, setType, resp.StatusCode, humanize.Bytes(uint64(bodySize)), duration)
	return nil
}

func main() {
	const limit = 100
	const dataDir = "./data"
	const imageDir = "./images"

	// Ensure the data and images directories exist
	err := os.MkdirAll(dataDir, os.ModePerm)
	if err != nil {
		logErrorAndExit(fmt.Errorf("failed to create data directory: %w", err))
	}

	err = os.MkdirAll(imageDir, os.ModePerm)
	if err != nil {
		logErrorAndExit(fmt.Errorf("failed to create images directory: %w", err))
	}

	// Fetch and save beta cards and images
	saveCards("bet", "beta", limit, dataDir, imageDir)

	// Fetch and save alpha cards and images
	saveCards("alp", "alpha", limit, dataDir, imageDir)
}

// saveCards fetches all cards for the given set type and saves them to files and images.
func saveCards(apiSetType, filePrefix string, limit int, dataDir, imageDir string) {
	allCards, err := fetchAllCards(limit, apiSetType)
	if err != nil {
		logErrorAndExit(err)
	}

	// Write all cards to cards.json in the data directory
	filename := fmt.Sprintf("%s/%s_cards.json", dataDir, filePrefix)
	writeCardsToFile(filename, allCards)

	// Download images for each variant and save to the appropriate directory
	for _, card := range allCards {
		for _, variant := range card.Variants {
			imageFilename := getImageFilename(variant.Slug)
			imagePath := filepath.Join(imageDir, apiSetType, imageFilename)

			err = os.MkdirAll(filepath.Dir(imagePath), os.ModePerm)
			if err != nil {
				logErrorAndExit(fmt.Errorf("failed to create image directory: %w", err))
			}

			err = downloadImage(variant.Src, imagePath, apiSetType)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Failed to download image %s: %v\n", imagePath, err)
			}
		}
	}

	// Group cards by element and write to files
	groupedByElement := groupCardsByElement(allCards)
	for element, cards := range groupedByElement {
		filename := fmt.Sprintf("%s/%s_%s_cards.json", dataDir, filePrefix, strings.ToLower(element))
		writeCardsToFile(filename, cards)
	}

	// Group cards by element, rarity, and type and write to files
	groupedByElementRarityType := groupCardsByElementRarityType(allCards)
	for key, cards := range groupedByElementRarityType {
		filename := fmt.Sprintf("%s/%s_%s_cards.json", dataDir, filePrefix, strings.ToLower(key))
		writeCardsToFile(filename, cards)
	}

	fmt.Printf("All cards have been written to %s_cards.json. Total cards: %d\n", filePrefix, len(allCards))
}

// setHeaders sets the necessary headers for the HTTP request.
func setHeaders(req *http.Request) {
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0")
	req.Header.Set("Accept", "*/*")
	req.Header.Set("content-type", "application/json")
}

// logErrorAndExit logs the error to stderr and exits with a non-zero status code.
func logErrorAndExit(err error) {
	fmt.Fprintln(os.Stderr, err)
	os.Exit(1)
}

// writeCardsToFile writes the given cards to a JSON file with the given filename.
func writeCardsToFile(filename string, cards []*Card) {
	file, err := os.Create(filename)
	if err != nil {
		logErrorAndExit(fmt.Errorf("failed to create %s: %w", filename, err))
	}
	defer file.Close()

	prettyData, err := json.MarshalIndent(cards, "", "  ")
	if err != nil {
		logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err))
	}

	_, err = file.Write(prettyData)
	if err != nil {
		logErrorAndExit(fmt.Errorf("failed to write to %s: %w", filename, err))
	}

	fmt.Printf("%s has been written. Total cards: %d\n", filename, len(cards))
}

// groupCardsByElement groups the given cards by their element.
func groupCardsByElement(cards []*Card) map[string][]*Card {
	grouped := make(map[string][]*Card)
	for _, card := range cards {
		for _, element := range card.Elements {
			grouped[element.Name] = append(grouped[element.Name], card)
		}
	}
	return grouped
}

// groupCardsByElementRarityType groups the given cards by their element, rarity, and type.
func groupCardsByElementRarityType(cards []*Card) map[string][]*Card {
	grouped := make(map[string][]*Card)
	for _, card := range cards {
		for _, element := range card.Elements {
			key := fmt.Sprintf("%s_%s_%s", element.Name, strings.ToLower(card.Guardian.Rarity), strings.ToLower(card.Guardian.Type))
			grouped[key] = append(grouped[key], card)
		}
	}
	return grouped
}

// getImageFilename constructs a human-readable image filename from the slug.
func getImageFilename(slug string) string {
	parts := strings.Split(slug, "_")
	name := strings.Join(parts[1:len(parts)-1], "_")
	variant := parts[len(parts)-1]

	switch variant {
	case "s":
		return fmt.Sprintf("%s_standard.jpg", name)
	case "f":
		return fmt.Sprintf("%s_foil.jpg", name)
	case "p":
		return fmt.Sprintf("%s_promo.jpg", name)
	default:
		return fmt.Sprintf("%s.jpg", name)
	}
}
