package api

import (
	"encoding/json"
	"net/http"

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

type CopyrightImpl interface {
	Search(*CopyrightSearchReq) (*CopyrightSearchResp, error)
	SearchPath(*CopyrightSearchPathReq) (*CopyrightSearchPathResp, error)
	SearchBatch(*CopyrightSearchBatchReq) (*CopyrightSearchBatchResp, error)

	List(*CopyrightListReq) (*CopyrightListResp, error)
	ListPrefix(*CopyrightListPrefixReq) (*CopyrightListPrefixResp, error)

	Status() *PingResp
}

type CopyrightSearchReq struct{}
type CopyrightSearchResp struct{}
type CopyrightSearchPathReq struct{}
type CopyrightSearchPathResp struct{}

type CopyrightSearchBatchReq struct{}
type CopyrightSearchBatchResp struct{}

type CopyrightListReq struct{}
type CopyrightListResp struct{}

type CopyrightListPrefixReq struct{}
type CopyrightListPrefixResp struct{}

func (a *ApiHandler) RouteCopyright(r chi.Router) {
	//search
	r.Get("/sha256", func(w http.ResponseWriter, r *http.Request) {
		req := &CopyrightSearchReq{}
		returnJson(w, req, a.copyright.Search)
	})
	r.Post("/sha256", func(w http.ResponseWriter, r *http.Request) {
		req := &CopyrightSearchBatchReq{}
		returnJson(w, req, a.copyright.SearchBatch)
	})
	r.Get("/file/{packagename}/{version}/*", func(w http.ResponseWriter, r *http.Request) {
		req := &CopyrightSearchPathReq{}
		returnJson(w, req, a.copyright.SearchPath)
	})

	//list
	r.Get("/list", func(w http.ResponseWriter, r *http.Request) {
		req := &CopyrightListReq{}
		returnJson(w, req, a.copyright.List)
	})
	r.Get("/prefix/{prefix}", func(w http.ResponseWriter, r *http.Request) {
		req := &CopyrightListPrefixReq{}
		returnJson(w, req, a.copyright.ListPrefix)
	})
	// status

	r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(a.copyright.Status())
	})
}
