package dal

import (
	"context"

	"github.guxuan/haibei/internal/mods/activity/schema"
	"github.guxuan/haibei/pkg/errors"
	"github.guxuan/haibei/pkg/util"
	"gorm.io/gorm"
)

// Get activity detail storage instance
func GetActivityDetailDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.ActivityDetail))
}

// Defining the `ActivityDetail` data access object.
type ActivityDetail struct {
	DB *gorm.DB
}

// Query activity details from the database based on the provided parameters and options.
func (a *ActivityDetail) Query(ctx context.Context, params schema.ActivityDetailQueryParam, opts ...schema.ActivityDetailQueryOptions) (*schema.ActivityDetailQueryResult, error) {
	var opt schema.ActivityDetailQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := GetActivityDetailDB(ctx, a.DB)

	var list schema.ActivityDetails
	pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	queryResult := &schema.ActivityDetailQueryResult{
		PageResult: pageResult,
		Data:       list,
	}
	return queryResult, nil
}

// Get the specified activity detail from the database.
func (a *ActivityDetail) Get(ctx context.Context, id uint, opts ...schema.ActivityDetailQueryOptions) (*schema.ActivityDetail, error) {
	var opt schema.ActivityDetailQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.ActivityDetail)
	ok, err := util.FindOne(ctx, GetActivityDetailDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

func (a *ActivityDetail) GetByCustomerIDAndActivityID(ctx context.Context, customerID, activityID uint) (*schema.ActivityDetail, error) {
	var opt schema.ActivityDetailQueryOptions

	item := new(schema.ActivityDetail)
	ok, err := util.FindOne(ctx, GetActivityDetailDB(ctx, a.DB).Where("customer_id =? AND activity_id = ? ", customerID, activityID), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Exists checks if the specified activity detail exists in the database.
func (a *ActivityDetail) Exists(ctx context.Context, id uint) (bool, error) {
	ok, err := util.Exists(ctx, GetActivityDetailDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

// Create a new activity detail.
func (a *ActivityDetail) Create(ctx context.Context, item *schema.ActivityDetail) error {
	result := GetActivityDetailDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

// Update the specified activity detail in the database.
func (a *ActivityDetail) Update(ctx context.Context, item *schema.ActivityDetail) error {
	result := GetActivityDetailDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
	return errors.WithStack(result.Error)
}

// Delete the specified activity detail from the database.
func (a *ActivityDetail) Delete(ctx context.Context, id uint) error {
	result := GetActivityDetailDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.ActivityDetail))
	return errors.WithStack(result.Error)
}
