package product

import (
	"context"

	"github.guxuan/haibei/internal/config"

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

type Product struct {
	DB                 *gorm.DB
	ProductAPI         *api.Product
	ProductCategoryAPI *api.ProductCategory
}

func (a *Product) AutoMigrate(ctx context.Context) error {
	return a.DB.AutoMigrate(new(schema.Product), new(schema.ProductCategory))
}

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

func (a *Product) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	product := v1.Group("products")
	{
		product.GET("", a.ProductAPI.Query)
		product.GET(":id", a.ProductAPI.Get)
		product.POST("", a.ProductAPI.Create)
		product.PUT(":id", a.ProductAPI.Update)
		product.DELETE(":id", a.ProductAPI.Delete)
	}
	productCategory := v1.Group("product-categories")
	{
		productCategory.GET("", a.ProductCategoryAPI.Query)
		productCategory.GET(":id", a.ProductCategoryAPI.Get)
		productCategory.POST("", a.ProductCategoryAPI.Create)
		productCategory.PUT(":id", a.ProductCategoryAPI.Update)
		productCategory.DELETE(":id", a.ProductCategoryAPI.Delete)
	}
	return nil
}

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