package logic

import (
	"context"
	"errors"
	"time"

	"juwan-backend/app/order/rpc/internal/models/orders"
	"juwan-backend/app/order/rpc/internal/models/predicate"
	"juwan-backend/app/order/rpc/internal/svc"
	"juwan-backend/app/order/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchOrdersLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchOrdersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchOrdersLogic {
	return &SearchOrdersLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchOrdersLogic) SearchOrders(in *pb.SearchOrdersReq) (*pb.SearchOrdersResp, error) {
	if in.Limit <= 0 {
		in.Limit = 20
	}
	if in.Limit > 100 {
		return nil, errors.New("limit too large")
	}
	if in.Offset < 0 {
		in.Offset = 0
	}

	preds := make([]predicate.Orders, 0, 20)
	if in.Id != nil {
		preds = append(preds, orders.IDEQ(*in.Id))
	}
	if in.ConsumerId != nil {
		preds = append(preds, orders.ConsumerIDEQ(*in.ConsumerId))
	}
	if in.PlayerId != nil {
		preds = append(preds, orders.PlayerIDEQ(*in.PlayerId))
	}
	if in.ShopId != nil {
		preds = append(preds, orders.ShopIDEQ(*in.ShopId))
	}
	if in.Status != nil && *in.Status != "" {
		preds = append(preds, orders.StatusEQ(*in.Status))
	}
	if in.TotalPrice != nil && *in.TotalPrice != "" {
		price, err := parseDecimal(*in.TotalPrice)
		if err != nil {
			return nil, err
		}
		preds = append(preds, orders.TotalPriceEQ(price))
	}
	if in.Note != nil && *in.Note != "" {
		preds = append(preds, orders.NoteContainsFold(*in.Note))
	}
	if in.Version != nil {
		preds = append(preds, orders.VersionEQ(int(*in.Version)))
	}
	if in.TimeoutJobId != nil && *in.TimeoutJobId != "" {
		preds = append(preds, orders.TimeoutJobIDEQ(*in.TimeoutJobId))
	}
	if in.SearchText != nil && *in.SearchText != "" {
		preds = append(preds, orders.SearchTextContainsFold(*in.SearchText))
	}
	if in.CreatedAt != nil && *in.CreatedAt > 0 {
		preds = append(preds, orders.CreatedAtGTE(time.Unix(*in.CreatedAt, 0)))
	}
	if in.AcceptedAt != nil && *in.AcceptedAt > 0 {
		preds = append(preds, orders.AcceptedAtGTE(time.Unix(*in.AcceptedAt, 0)))
	}
	if in.ClosedAt != nil && *in.ClosedAt > 0 {
		preds = append(preds, orders.ClosedAtGTE(time.Unix(*in.ClosedAt, 0)))
	}
	if in.CompletedAt != nil && *in.CompletedAt > 0 {
		preds = append(preds, orders.CompletedAtGTE(time.Unix(*in.CompletedAt, 0)))
	}
	if in.CancelledAt != nil && *in.CancelledAt > 0 {
		preds = append(preds, orders.CancelledAtGTE(time.Unix(*in.CancelledAt, 0)))
	}
	if in.UpdatedAt != nil && *in.UpdatedAt > 0 {
		preds = append(preds, orders.UpdatedAtGTE(time.Unix(*in.UpdatedAt, 0)))
	}

	query := l.svcCtx.OrderModelsRO.Orders.Query()
	if len(preds) > 0 {
		query = query.Where(orders.And(preds...))
	}

	items, err := query.Offset(int(in.Offset)).Limit(int(in.Limit)).All(l.ctx)
	if err != nil {
		return nil, err
	}

	result := make([]*pb.Orders, 0, len(items))
	for _, item := range items {
		result = append(result, toPBOrder(item))
	}

	return &pb.SearchOrdersResp{Orders: result}, nil
}
