package booking

import (
	"log"

	"github.com/rjNemo/rentease/internal/config"
)

func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, price float64, paymentMethod string, customerNumber int, platform string) (items []*Item) {
	i := &Item{
		BookingId:     bookingId,
		Item:          item.Name,
		Quantity:      quantity,
		Price:         price,
		PaymentMethod: paymentMethod,
	}
	if err := bs.store.CreateItem(i); err != nil {
		log.Println(err)
	}
	items = append(items, i)

	if item.Taxes != 0.0 && platform == "Booking" {
		ti := &Item{
			BookingId:     bookingId,
			Item:          "Taxes",
			Quantity:      quantity * customerNumber,
			Price:         item.Taxes,
			PaymentMethod: "Cash",
		}
		if err := bs.store.CreateItem(ti); err != nil {
			log.Println(err)
		}
		items = append(items, ti)
	}

	return items
}

func (bs Service) PayItem(id int) *Item {
	i, err := bs.store.PayItem(id)
	if err != nil {
		log.Println(err)
	}
	return i
}

func (bs Service) OneItem(id int) *Item {
	i, err := bs.store.GetItem(id)
	if err != nil {
		log.Println(err)
	}
	return i
}

func (bs Service) UpdateItem(id int, item string, qty int, price float64, paymentMethod, paymentStatus string) *Item {
	i, err := bs.store.UpdateItem(id, item, paymentMethod, paymentStatus, qty, price)
	if err != nil {
		log.Println(err)
	}
	return i
}
