package validation

import (
	"errors"
	"strings"

	"gitlab.com/mbugroup/lti-api.git/internal/utils"
	approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
)

type Create struct {
	CustomerId        uint                     `json:"customer_id" validate:"required,gt=0"`
	SalesPersonId     uint                     `json:"sales_person_id" validate:"required,gt=0"`
	Date              string                   `json:"date" validate:"required,datetime=2006-01-02"`
	Notes             string                   `json:"notes" validate:"omitempty,max=500"`
	MarketingProducts []CreateMarketingProduct `json:"marketing_products" validate:"required,min=1,dive"`
}

type CreateMarketingProduct struct {
	MarketingType       string   `json:"marketing_type" validate:"required,min=1,max=50"`
	VehicleNumber       string   `json:"vehicle_number" validate:"required,min=1,max=50"`
	ConvertionUnit      *string  `json:"convertion_unit" validate:"omitempty,min=1,max=20"`
	WeightPerConvertion *float64 `json:"weight_per_convertion" validate:"omitempty,gt=0"`
	Week                *int     `json:"week" validate:"omitempty,gt=0"`
	ProductWarehouseId  uint     `json:"product_warehouse_id" validate:"required,gt=0"`
	UnitPrice           float64  `json:"unit_price" validate:"required,gt=0"`
	Qty                 float64  `json:"qty" validate:"required,gt=0"`
	AvgWeight           float64  `json:"avg_weight" validate:"omitempty,gt=0"`
}

type Update struct {
	CustomerId        uint                     `json:"customer_id" validate:"omitempty,gt=0"`
	SalesPersonId     uint                     `json:"sales_person_id" validate:"omitempty,gt=0"`
	Date              string                   `json:"date" validate:"omitempty,datetime=2006-01-02"`
	Notes             string                   `json:"notes" validate:"omitempty,max=500"`
	MarketingProducts []CreateMarketingProduct `json:"marketing_products" validate:"omitempty,min=1,dive"`
}

type Approve struct {
	Action        string  `json:"action" validate:"required_strict"`
	ApprovableIds []uint  `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
	Notes         *string `json:"notes,omitempty" validate:"omitempty,max=500"`
}

type BulkApprovalRequest struct {
	ApprovableIds []uint  `json:"approvable_ids" validate:"required,min=1,dive,gt=0"`
	Status        string  `json:"status" validate:"required,max=100"`
	Date          string  `json:"date,omitempty" validate:"omitempty,datetime=2006-01-02"`
	Notes         *string `json:"notes,omitempty" validate:"omitempty,max=500"`
}

func (r *BulkApprovalRequest) ResolveTarget() (approvalutils.ApprovalStep, error) {
	status := strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(r.Status), " ", "_"))

	switch status {
	case "SALES_ORDER":
		return utils.MarketingStepSalesOrder, nil
	case "DELIVERY", "DELIVERY_ORDER":
		return utils.MarketingDeliveryOrder, nil
	default:
		return 0, errors.New("status must be one of SALES_ORDER or DELIVERY")
	}
}

func (r *BulkApprovalRequest) RequiresDate(target approvalutils.ApprovalStep) bool {
	return target == utils.MarketingDeliveryOrder
}
