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 qeustion detail storage instance
func GetQeustionDetailDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.QeustionDetail))
}

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

// Query qeustion details from the database based on the provided parameters and options.
func (a *QeustionDetail) Query(ctx context.Context, params schema.QeustionDetailQueryParam, opts ...schema.QeustionDetailQueryOptions) (*schema.QeustionDetailQueryResult, error) {
	var opt schema.QeustionDetailQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := GetQeustionDetailDB(ctx, a.DB)

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

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

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

	item := new(schema.QeustionDetail)
	ok, err := util.FindOne(ctx, GetQeustionDetailDB(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 *QeustionDetail) GetByCustomerIDAndActivityID(ctx context.Context, customerID, questionID uint) (*schema.QeustionDetail, error) {
	var opt schema.QeustionDetailQueryOptions

	item := new(schema.QeustionDetail)
	ok, err := util.FindOne(ctx, GetQeustionDetailDB(ctx, a.DB).Where("customer_id =? AND question_id = ? ", customerID, questionID), 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 qeustion detail exists in the database.
func (a *QeustionDetail) Exists(ctx context.Context, id uint) (bool, error) {
	ok, err := util.Exists(ctx, GetQeustionDetailDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

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

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

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