package internal

import (
	"encoding/json"
	"fmt"
	"hakushi-crawl/api"
	"os"
	"sync"
)

func SaveDataMonster(locale string) error {
	listItem, err := api.GetListMonsterApi()
	if err != nil {
		return err
	}

	mapItemDetail := make(map[string]*any)

	var mu sync.Mutex

	jobs := make(chan string)
	wg := sync.WaitGroup{}

	workerCount := 12

	for range workerCount {
		wg.Go(func() {
			for itemId := range jobs {
				itemDetail, err := api.GetMonsterInfoApi(itemId, locale)
				if err != nil || itemDetail == nil {
					continue
				}
				mu.Lock()
				mapItemDetail[itemId] = itemDetail
				mu.Unlock()
			}
		})
	}

	for itemId := range listItem {
		jobs <- itemId
	}
	close(jobs)

	wg.Wait()

	fileName := fmt.Sprintf("monsters.%s.json", locale)

	data, err := json.Marshal(mapItemDetail)
	if err != nil {
		return err
	}

	return os.WriteFile(fileName, data, 0644)
}
