package api

import (
	"fmt"
	"net/http"
	"strings"

	"arimelody-web/admin"
	"arimelody-web/global"
	"arimelody-web/controller"
)

func Handler() http.Handler {
    mux := http.NewServeMux()

    // ARTIST ENDPOINTS

    mux.Handle("/v1/artist/", http.StripPrefix("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        var artistID = strings.Split(r.URL.Path[1:], "/")[0]
        artist, err := controller.GetArtist(global.DB, artistID)
        if err != nil {
            if strings.Contains(err.Error(), "no rows") {
                http.NotFound(w, r)
                return
            }
            fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err)
            http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
            return
        }

        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/artist/{id}
            ServeArtist(artist).ServeHTTP(w, r)
        case http.MethodPut:
            // PUT /api/v1/artist/{id} (admin)
            admin.MustAuthorise(UpdateArtist(artist)).ServeHTTP(w, r)
        case http.MethodDelete:
            // DELETE /api/v1/artist/{id} (admin)
            admin.MustAuthorise(DeleteArtist(artist)).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    })))
    mux.Handle("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/artist
            ServeAllArtists().ServeHTTP(w, r)
        case http.MethodPost:
            // POST /api/v1/artist (admin)
            admin.MustAuthorise(CreateArtist()).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    }))

    // RELEASE ENDPOINTS

    mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        var releaseID = strings.Split(r.URL.Path[1:], "/")[0]
        release, err := controller.GetRelease(global.DB, releaseID, true)
        if err != nil {
            if strings.Contains(err.Error(), "no rows") {
                http.NotFound(w, r)
                return
            }
            fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err)
            http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
            return
        }

        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/music/{id}
            ServeRelease(release).ServeHTTP(w, r)
        case http.MethodPut:
            // PUT /api/v1/music/{id} (admin)
            admin.MustAuthorise(UpdateRelease(release)).ServeHTTP(w, r)
        case http.MethodDelete:
            // DELETE /api/v1/music/{id} (admin)
            admin.MustAuthorise(DeleteRelease(release)).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    })))
    mux.Handle("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/music
            ServeCatalog().ServeHTTP(w, r)
        case http.MethodPost:
            // POST /api/v1/music (admin)
            admin.MustAuthorise(CreateRelease()).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    }))

    // TRACK ENDPOINTS

    mux.Handle("/v1/track/", http.StripPrefix("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        var trackID = strings.Split(r.URL.Path[1:], "/")[0]
        track, err := controller.GetTrack(global.DB, trackID)
        if err != nil {
            if strings.Contains(err.Error(), "no rows") {
                http.NotFound(w, r)
                return
            }
            fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err)
            http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
            return
        }

        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/track/{id} (admin)
            admin.MustAuthorise(ServeTrack(track)).ServeHTTP(w, r)
        case http.MethodPut:
            // PUT /api/v1/track/{id} (admin)
            admin.MustAuthorise(UpdateTrack(track)).ServeHTTP(w, r)
        case http.MethodDelete:
            // DELETE /api/v1/track/{id} (admin)
            admin.MustAuthorise(DeleteTrack(track)).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    })))
    mux.Handle("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        switch r.Method {
        case http.MethodGet:
            // GET /api/v1/track (admin)
            admin.MustAuthorise(ServeAllTracks()).ServeHTTP(w, r)
        case http.MethodPost:
            // POST /api/v1/track (admin)
            admin.MustAuthorise(CreateTrack()).ServeHTTP(w, r)
        default:
            http.NotFound(w, r)
        }
    }))

    return mux
}
