package biz

import (
	"context"
	"fmt"
	"github.com/skip2/go-qrcode"
	"github.guxuan/haibei/internal/mods/common/dal"
	"github.guxuan/haibei/internal/mods/common/schema"
	"github.guxuan/haibei/pkg/errors"
	"github.guxuan/haibei/pkg/util"
	"time"
)

type ReceptionCenter struct {
	Trans              *util.Trans
	ReceptionCenterDAL *dal.ReceptionCenter
}

// Query companies from the data access object based on the provided parameters and options.
func (a *ReceptionCenter) Query(ctx context.Context, params schema.ReceptionCenterQueryParam) (*schema.ReceptionCenterQueryResult, error) {
	params.Pagination = true

	result, err := a.ReceptionCenterDAL.Query(ctx, params, schema.ReceptionCenterQueryOptions{
		QueryOptions: util.QueryOptions{
			OrderFields: []util.OrderByParam{
				{Field: "created_at", Direction: util.DESC},
			},
		},
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Get the specified company from the data access object.
func (a *ReceptionCenter) Get(ctx context.Context, id uint) (*schema.ReceptionCenter, error) {
	company, err := a.ReceptionCenterDAL.Get(ctx, id)
	if err != nil {
		return nil, err
	} else if company == nil {
		return nil, errors.NotFound("", "Company not found")
	}
	return company, nil
}
func GenerateStoreQR(recepId uint) ([]byte, error) {
	// 格式：checkin:store_id=123 (添加前缀防止恶意扫描)
	content := fmt.Sprintf("checkin:reception_center_id=%d", recepId)
	return qrcode.Encode(content, qrcode.Medium, 256)
}
func (a *ReceptionCenter) GetQrCode(ctx context.Context, id uint) ([]byte, error) {
	recep, err := a.ReceptionCenterDAL.Get(ctx, id)
	if err != nil {
		return nil, err
	} else if recep == nil {
		return nil, errors.NotFound("", "ReceptionCenter not found")
	}
	png, err := GenerateStoreQR(recep.ID)
	if err != nil {
		return nil, err

	}
	return png, nil
}

// Create a new company in the data access object.
func (a *ReceptionCenter) Create(ctx context.Context, formItem *schema.ReceptionCenterForm) (*schema.ReceptionCenter, error) {
	recep := &schema.ReceptionCenter{}

	if err := formItem.FillTo(recep); err != nil {
		return nil, err
	}

	err := a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ReceptionCenterDAL.Create(ctx, recep); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return recep, nil
}

func (a *ReceptionCenter) CheckQrcode(ctx context.Context, receptionID, customerID uint) error {
	recep, err := a.ReceptionCenterDAL.Get(ctx, receptionID)
	if err != nil {
		return err
	} else if recep == nil {
		return errors.NotFound("", "ReceptionCenter not found")
	}
	return nil
}

// Update the specified company in the data access object.
func (a *ReceptionCenter) Update(ctx context.Context, id uint, formItem *schema.ReceptionCenterForm) error {
	recep, err := a.ReceptionCenterDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if recep == nil {
		return errors.NotFound("", "recep not found")
	}

	if err := formItem.FillTo(recep); err != nil {
		return err
	}
	recep.UpdatedAt = time.Now()

	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ReceptionCenterDAL.Update(ctx, recep); err != nil {
			return err
		}
		return nil
	})
}

// Delete the specified company from the data access object.
func (a *ReceptionCenter) Delete(ctx context.Context, id uint) error {
	exists, err := a.ReceptionCenterDAL.Exists(ctx, id)
	if err != nil {
		return err
	} else if !exists {
		return errors.NotFound("", "recep not found")
	}

	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ReceptionCenterDAL.Delete(ctx, id); err != nil {
			return err
		}
		return nil
	})
}
