package activity

import (
	"context"

	"github.guxuan/haibei/internal/config"

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

type Activity struct {
	DB                *gorm.DB
	ActivityAPI       *api.Activity
	QuestionnaireAPI  *api.Questionnaire
	ActivityDetailAPI *api.ActivityDetail
	QeustionDetailAPI *api.QeustionDetail
}

func (a *Activity) AutoMigrate(ctx context.Context) error {
	return a.DB.AutoMigrate(new(schema.Activity), new(schema.Questionnaire), new(schema.ActivityDetail), new(schema.QeustionDetail), new(schema.House), new(schema.HouseArticle))
}

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

func (a *Activity) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	activity := v1.Group("activities")
	{
		activity.GET("", a.ActivityAPI.Query)
		activity.GET(":id", a.ActivityAPI.Get)
		activity.POST("", a.ActivityAPI.Create)
		activity.PUT(":id", a.ActivityAPI.Update)
		activity.DELETE(":id", a.ActivityAPI.Delete)
	}
	questionnaire := v1.Group("questionnaires")
	{
		questionnaire.GET("", a.QuestionnaireAPI.Query)
		questionnaire.GET(":id", a.QuestionnaireAPI.Get)
		questionnaire.POST("", a.QuestionnaireAPI.Create)
		questionnaire.PUT(":id", a.QuestionnaireAPI.Update)
		questionnaire.DELETE(":id", a.QuestionnaireAPI.Delete)
	}
	activityDetail := v1.Group("activity-details")
	{
		activityDetail.GET("", a.ActivityDetailAPI.Query)
		activityDetail.GET(":id", a.ActivityDetailAPI.Get)
		activityDetail.POST("", a.ActivityDetailAPI.Create)
		activityDetail.PUT(":id", a.ActivityDetailAPI.Update)
		activityDetail.DELETE(":id", a.ActivityDetailAPI.Delete)
	}
	qeustionDetail := v1.Group("qeustion-details")
	{
		qeustionDetail.GET("", a.QeustionDetailAPI.Query)
		qeustionDetail.GET(":id", a.QeustionDetailAPI.Get)
		qeustionDetail.POST("", a.QeustionDetailAPI.Create)
		qeustionDetail.PUT(":id", a.QeustionDetailAPI.Update)
		qeustionDetail.DELETE(":id", a.QeustionDetailAPI.Delete)
	}
	return nil
}

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