package customer

import (
	"context"

	"github.guxuan/haibei/internal/config"

	"github.com/gin-gonic/gin"
	"github.guxuan/haibei/internal/mods/customer/api"
	"github.guxuan/haibei/internal/mods/customer/schema"
	"gorm.io/gorm"
)

type Customer struct {
	DB              *gorm.DB
	CustomerAPI     *api.Customer
	BalanceAPI      *api.Balance
	ProductOrderAPI *api.ProductOrder
}

func (a *Customer) AutoMigrate(ctx context.Context) error {
	return a.DB.AutoMigrate(new(schema.Customer), new(schema.Balance), new(schema.ProductOrder))
}

func (a *Customer) Init(ctx context.Context) error {
	if config.C.Storage.DB.AutoMigrate {
		if err := a.AutoMigrate(ctx); err != nil {
			return err
		}
	}
	return nil
}

func (a *Customer) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	customer := v1.Group("customers")
	{
		customer.GET("", a.CustomerAPI.Query)
		customer.GET(":id", a.CustomerAPI.Get)
		customer.POST("", a.CustomerAPI.Create)
		customer.PUT(":id", a.CustomerAPI.Update)
		customer.DELETE(":id", a.CustomerAPI.Delete)
	}
	balance := v1.Group("balances")
	{
		balance.GET("", a.BalanceAPI.Query)
		balance.GET(":id", a.BalanceAPI.Get)
		balance.POST("", a.BalanceAPI.Create)
		balance.PUT(":id", a.BalanceAPI.Update)
		balance.DELETE(":id", a.BalanceAPI.Delete)
	}
	productOrder := v1.Group("product-orders")
	{
		productOrder.GET("", a.ProductOrderAPI.Query)
		productOrder.GET(":id", a.ProductOrderAPI.Get)
		productOrder.POST("", a.ProductOrderAPI.Create)
		productOrder.PUT(":id", a.ProductOrderAPI.Update)
		productOrder.DELETE(":id", a.ProductOrderAPI.Delete)
	}
	return nil
}

func (a *Customer) Release(ctx context.Context) error {
	return nil
}
