package main

import (
	"fmt"
//	"math/rand"
	"os"
//	"strings"
//	"time"
	"adventofcode2024/utils"
	"adventofcode2024/day01"
	"adventofcode2024/day02"
	"adventofcode2024/day03"
	"adventofcode2024/day04"

)
// Usage: go run main.go <NN>
// assumes input is in day<NN>/input.txt
func main() {
	d := day()
	fmt.Printf("Running day %02d\n", d)

	switch d {
	case 1:
		fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
	case 2:
		fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
	case 3:
		fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
	case 4:
		fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d)))
	default:
		panic(fmt.Errorf("no such day: %d", d))
	}
}

// Reads day from os.Args.
func day() int {
	latest := 3
	if len(os.Args) == 1 {
		return latest
	}

	if os.Args[1] == "next" {
		genNext(latest + 1)
		os.Exit(0)
	}
	day := utils.MustAtoi(os.Args[1])
	return day
}


func genNext(n int) {
	os.Mkdir(fmt.Sprintf("day%02d", n), 0755)
	f, err := os.Create(fmt.Sprintf("day%02d/day%02d.go", n, n))
	utils.PanicOnErr(err)
	defer f.Close()
	f.WriteString(fmt.Sprintf(`package day%02d

func Part1(input string) int {
	return 0
}

func Part2(input string) int {
	return 0
}
`, n))
	fmt.Printf("wrote day%02d/day%02d.go\n", n, n)

	f, err = os.Create(fmt.Sprintf("day%02d/day%02d_test.go", n, n))
	utils.PanicOnErr(err)
	defer f.Close()
	f.WriteString(fmt.Sprintf(`package day%02d

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestPart1(t *testing.T) {
	r := Part1("")
	require.Equal(t, 0, r)
}

func TestPart2(t *testing.T) {
	r := Part2("")
	require.Equal(t, 0, r)
}
`, n))
	fmt.Printf("wrote day%02d/day%02d_test.go\n", n, n)
	utils.GenInputFile(n)

}