package main

import (
	"butterfliu/internal/controller"
	"butterfliu/internal/repository"
	"butterfliu/internal/service"
	"log"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	err := InitDB("./butterfliu.db")
	if err != nil {
		log.Fatal(err)
	}

	r := chi.NewRouter()
	r.Use(middleware.Logger)

	libraryRepo := repository.NewLibraryRepository(GetDB())
	libraryService := service.NewLibraryService(libraryRepo)
	libraryController := controller.NewLibraryController(libraryService)

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!"))
	})

	r.Route("/api/libraries", func(r chi.Router) {
		r.Get("/", libraryController.GetAll)
		r.Post("/", libraryController.Create)
		r.Put("/{id}/name", libraryController.UpdateName)
		r.Put("/{id}/path", libraryController.UpdatePath)
		r.Post("/{id}/scan", libraryController.Scan)
		r.Get("/scan-status", libraryController.GetScanStatus)
		r.Delete("/{id}", libraryController.Delete)
		r.Get("/{id}/songs", libraryController.GetSongs)
	})

	r.Route("/api", func(r chi.Router) {
		r.Get("/songs", libraryController.GetAllSongs)
		r.Get("/artists", libraryController.GetArtists)
		r.Get("/albums", libraryController.GetAlbums)
	})

	http.ListenAndServe(":8102", r)
}
