package db

import (
	"fmt"
	"music-server/pkg/models"
	"os"
	"time"
)

func InsertSongInList(song models.SongListData) {
	_, err := Dbpool.Exec(Ctx,
		`INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name) VALUES ($1, $2, $3, $4, $5)`,
		song.MatchDate, song.MatchId, song.SongNo, song.GameName, song.SongName)
	if err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
	}
}

func GetSongList(matchId int) []models.SongListData {
	rows, err := Dbpool.Query(Ctx,
		"SELECT match_date, match_id, song_no, game_name, song_name "+
			"FROM song_list WHERE match_date = $1"+
			"ORDER BY song_no DESC", matchId)
	if err != nil {
		if compareError.Error() != err.Error() {
			_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
		}
	}
	var songList []models.SongListData
	for rows.Next() {
		var matchId, songNo int
		var matchDate time.Time
		var gameName, songName string
		err := rows.Scan(&matchDate, &matchId, &songNo, &gameName, &songName)
		if err != nil {
			_, _ = fmt.Fprintf(os.Stderr, "Scan failed: %v\n", err)
		}
		songList = append(songList, models.SongListData{
			MatchDate: matchDate,
			MatchId:   matchId,
			SongNo:    songNo,
			GameName:  gameName,
			SongName:  songName,
		})
	}
	return songList
}
