package server

import (
	"fmt"
	"local/router"
	"net/http"
)

func (s *Server) Routes() error {
	wildcard := router.Wildcard
	endpoints := []struct {
		path    string
		handler http.HandlerFunc
	}{
		{
			path:    fmt.Sprintf("/api/job/get/%s", router.Wildcard),
			handler: s.gzip(s.authenticate(s.get)),
		},
		{
			path:    fmt.Sprintf("/api/job/upsert"),
			handler: s.gzip(s.authenticate(s.upsert)),
		},
		{
			path:    fmt.Sprintf("/api/job/list"),
			handler: s.gzip(s.authenticate(s.list)),
		},
		{
			path:    fmt.Sprintf("/api/job/delete/%s", router.Wildcard),
			handler: s.gzip(s.authenticate(s.delete)),
		},
		{
			path:    fmt.Sprintf("/api/job/disable/%s", router.Wildcard),
			handler: s.gzip(s.authenticate(s.disable)),
		},
		{
			path:    fmt.Sprintf("/api/job/enable/%s", router.Wildcard),
			handler: s.gzip(s.authenticate(s.enable)),
		},
		{
			path:    fmt.Sprintf("%s%s", wildcard, wildcard),
			handler: s.gzip(s.authenticate(s.static)),
		},
	}

	for _, endpoint := range endpoints {
		if err := s.Add(endpoint.path, endpoint.handler); err != nil {
			return err
		}
	}
	return nil
}

func (s *Server) static(w http.ResponseWriter, r *http.Request) {
	s.fileServer.ServeHTTP(w, r)
}
