package services

import (
	"nearle/models"
	"nearle/repositories"
)

type DeliveriesService interface {
	CreateDeliveriesService(data []models.Deliveries) error
	UpdateDeliveryService(data models.UpdateDeliveryStatus) error
	GetDeliverySummary(tid, pid, uid, aid, lid int, fdate, tdate string) (models.DeliverySummary, error)
	GetDeliveryInsightService(tid int) ([]models.OrderInsightv1, error)
	GetLocationDeliverySummary(tenantID int) ([]models.Ordersummarylocation, error)
	GetReportSummary(tid, pid, uid, aid int, fdate, tdate string) ([]models.ReportSummary, error)
	GetRiderSummary(aid, pid, tid int, fdate, tdate string) ([]models.Ridersummary, error)
	GetDeliveries(input models.DeliveryQuery) []models.Deliveryinfo
	GetDeliveryQueues(uid int, fdate, tdate string) ([]models.Deliveryinfo, error)
}

type deliveriesService struct {
	repo repositories.DeliveriesRepository
}

func NewDeliveriesService(repo repositories.DeliveriesRepository) DeliveriesService {
	return &deliveriesService{repo: repo}
}

func (s *deliveriesService) CreateDeliveriesService(data []models.Deliveries) error {
	return s.repo.CreateDeliveries(data)
}

func (s *deliveriesService) UpdateDeliveryService(data models.UpdateDeliveryStatus) error {
	return s.repo.UpdateDelivery(data)
}

func (s *deliveriesService) GetDeliverySummary(tid, pid, uid, aid, lid int, fdate, tdate string) (models.DeliverySummary, error) {
	return s.repo.GetDeliverySummary(tid, pid, uid, aid, lid, fdate, tdate)
}

func (s *deliveriesService) GetDeliveryInsightService(tid int) ([]models.OrderInsightv1, error) {
	return s.repo.GetDeliveryInsight(tid)
}

func (s *deliveriesService) GetLocationDeliverySummary(tenantID int) ([]models.Ordersummarylocation, error) {
	return s.repo.GetLocationDeliverySummary(tenantID)
}

func (s *deliveriesService) GetReportSummary(tid, pid, uid, aid int, fdate, tdate string) ([]models.ReportSummary, error) {
	return s.repo.GetReportSummary(tid, pid, uid, aid, fdate, tdate)
}

func (s *deliveriesService) GetRiderSummary(aid, pid, tid int, fdate, tdate string) ([]models.Ridersummary, error) {
	return s.repo.GetRiderSummary(aid, pid, tid, fdate, tdate)
}

func (s *deliveriesService) GetDeliveries(input models.DeliveryQuery) []models.Deliveryinfo {
	switch {
	case input.Tenantid != 0 && input.Locationid != 0:
		return s.repo.GetTenantLocationDeliveries(input) // 👈 NEW

	case input.Tenantid != 0:
		return s.repo.GetTenantDeliveries(input)

	case input.Partnerid != 0:
		return s.repo.GetPartnerDeliveries(input)

	case input.Customerid != 0:
		return s.repo.GetCustomerDeliveries(input)

	case input.Applocationid != 0:
		return s.repo.GetAdminDeliveries(input)

	case input.UserID != 0:
		return s.repo.GetUserDeliveries(input)

	case input.Appuserid != 0:
		return s.repo.GetAppUserDeliveries(input)

	default:
		return s.repo.GetDeliveries(input)
	}
}


func (s *deliveriesService) GetDeliveryQueues(uid int, fdate, tdate string) ([]models.Deliveryinfo, error) {
	return s.repo.GetDeliveryQueues(uid, fdate, tdate)
}
