package point

import (
	"context"

	"github.guxuan/haibei/internal/config"

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

type Point struct {
	DB       *gorm.DB
	PointAPI *api.Point
	GradeAPI *api.Grade
}

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

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

func (a *Point) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	point := v1.Group("points")
	{
		point.GET("", a.PointAPI.Query)
		point.GET(":id", a.PointAPI.Get)
		point.POST("", a.PointAPI.Create)
		point.PUT(":id", a.PointAPI.Update)
		point.DELETE(":id", a.PointAPI.Delete)
	}
	grade := v1.Group("grades")
	{
		grade.GET("", a.GradeAPI.Query)
		grade.GET(":id", a.GradeAPI.Get)
		grade.POST("", a.GradeAPI.Create)
		grade.PUT(":id", a.GradeAPI.Update)
		grade.DELETE(":id", a.GradeAPI.Delete)
	}
	return nil
}

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