package dal

import (
	"context"

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

// Get product order storage instance
func GetProductOrderDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.ProductOrder))
}

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

// Query product orders from the database based on the provided parameters and options.
func (a *ProductOrder) Query(ctx context.Context, params schema.ProductOrderQueryParam, opts ...schema.ProductOrderQueryOptions) (*schema.ProductOrderQueryResult, error) {
	var opt schema.ProductOrderQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := GetProductOrderDB(ctx, a.DB)

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

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

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

	item := new(schema.ProductOrder)
	ok, err := util.FindOne(ctx, GetProductOrderDB(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
}

// Exists checks if the specified product order exists in the database.
func (a *ProductOrder) ExistsByCustomerID(ctx context.Context, customerID, productId uint) (*schema.ProductOrders, error) {
	list := new(schema.ProductOrders)
	tx := GetProductOrderDB(ctx, a.DB).Where("customer_id = ? AND  product_id = ? ", customerID, productId).Find(list)
	if err := tx.Error; err != nil {
		return nil, errors.WithStack(err)
	}
	return list, nil
}

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

// Create a new product order.
func (a *ProductOrder) Create(ctx context.Context, item *schema.ProductOrder) error {
	result := GetProductOrderDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

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

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