package repository

import (
	"bpmsapi/entity"
	"log"

	"github.com/jmoiron/sqlx"
)

type PromoActions interface {
	FetchUserActivePromocode(userid int) ([]entity.Promocode, error)
}

type PromoRepository struct {
	db *sqlx.DB
}

func PromoRepositoryInit(db *sqlx.DB) *PromoRepository {
	return &PromoRepository{
		db: db,
	}
}

func (promoRepo *PromoRepository) FetchUserActivePromocode(userid int) (promocode []entity.Promocode, err error) {
	err = promoRepo.db.Select(&promocode, "SELECT p.Promocode_Id, p.Name, p.Discount, p.Max_Discount, p.Message, p.Color_Code from BPMS_PROMOCODE p INNER JOIN BPMS_PROMO_MAPPING pm on (p.Promocode_Id = pm.Promocode_Id AND pm.User_Id = ?) where pm.Is_Used = 0 AND pm.Is_Active = 1", userid)
	if err != nil {
		log.Print("Errpr", err.Error())
		return nil, err
	} else {
		return promocode, nil
	}
}
