package api

import (
	"log"

	"github.com/gorilla/mux"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type api struct {
	db      *gorm.DB
	signKey []byte
}

func initDB(dbPath string) (*gorm.DB, error) {
	db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
	if err != nil {
		return nil, err
	}

	db.AutoMigrate(&Member{}, &Product{}, &Purchase{}, &Topup{}, &Transaction{})
	return db, err
}

func Init(dbPath string, signKey string, r *mux.Router) error {
	db, err := initDB(dbPath)
	if err != nil {
		return err
	}

	a := api{db, []byte(signKey)}

	token, err := a.newToken(0, "admin", false)
	log.Print(token)

	r.HandleFunc("/signin", a.SignIn).Methods("POST")
	r.HandleFunc("/token", a.GetToken).Methods("GET")

	r.HandleFunc("/member", a.authAdmin(a.ListMembers)).Methods("GET")
	r.HandleFunc("/member", a.authAdmin(a.AddMember)).Methods("POST")
	r.HandleFunc("/member/me", a.authNum(a.getMemberNum)).Methods("GET")
	r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.GetMember)).Methods("GET")
	r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.UpdateMember)).Methods("PUT")
	r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.DeleteMember)).Methods("DELETE")
	r.HandleFunc("/member/{num:[0-9]+}/purchase", a.authAdmin(a.GetMemberTransactions)).Methods("GET")

	r.HandleFunc("/product", a.auth(a.ListProducts)).Methods("GET")
	r.HandleFunc("/product", a.authAdmin(a.AddProduct)).Methods("POST")
	r.HandleFunc("/product/{code:[0-9]+}", a.auth(a.GetProduct)).Methods("GET")
	r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.UpdateProduct)).Methods("PUT")
	r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.DeleteProduct)).Methods("DELETE")

	r.HandleFunc("/transaction", a.authAdmin(a.ListTransactions)).Methods("GET")
	r.HandleFunc("/transaction/{id:[0-9]+}", a.authNumRole(a.GetTransaction)).Methods("GET")
	r.HandleFunc("/transaction/mine", a.authNum(a.getTransactionsByMember)).Methods("GET")

	r.HandleFunc("/purchase", a.authNum(a.AddPurchase)).Methods("POST")
	r.HandleFunc("/topup", a.authAdminNum(a.AddTopup)).Methods("POST")
	return nil
}