package services

import (
	"time"

	"github.com/treytartt/honeydue-api/internal/models"
	"github.com/treytartt/honeydue-api/internal/task/predicates"
)

// iOS Notification Category Identifiers
const (
	IOSCategoryTaskActionable = "TASK_ACTIONABLE"   // overdue, due_soon, upcoming
	IOSCategoryTaskInProgress = "TASK_IN_PROGRESS"  // tasks in progress
	IOSCategoryTaskCancelled  = "TASK_CANCELLED"    // cancelled tasks
	IOSCategoryTaskCompleted  = "TASK_COMPLETED"    // completed tasks (read-only)
	IOSCategoryTaskGeneric    = "TASK_NOTIFICATION_GENERIC" // non-premium users
)

// GetButtonTypesForTask returns the appropriate button_types for a task.
// Uses predicates from internal/task/predicates as the single source of truth.
// Priority order matches kanban categorization chain.
func GetButtonTypesForTask(task *models.Task, daysThreshold int) []string {
	now := time.Now().UTC()

	// Priority order matches kanban logic (see categorization/chain.go)
	// 1. Cancelled
	if predicates.IsCancelled(task) {
		return []string{"uncancel", "delete"}
	}

	// 2. Completed (one-time task with nil next_due_date and has completions)
	if predicates.IsCompleted(task) {
		return []string{} // read-only
	}

	// 3. In Progress
	if predicates.IsInProgress(task) {
		return []string{"edit", "complete", "cancel"}
	}

	// 4. Overdue
	if predicates.IsOverdue(task, now) {
		return []string{"edit", "complete", "cancel", "mark_in_progress"}
	}

	// 5. Due Soon
	if predicates.IsDueSoon(task, now, daysThreshold) {
		return []string{"edit", "complete", "cancel", "mark_in_progress"}
	}

	// 6. Upcoming (default for tasks with future due dates or no due date)
	return []string{"edit", "complete", "cancel", "mark_in_progress"}
}

// GetIOSCategoryForTask returns the iOS notification category identifier.
// Uses predicates from internal/task/predicates as the single source of truth.
func GetIOSCategoryForTask(task *models.Task) string {
	if predicates.IsCancelled(task) {
		return IOSCategoryTaskCancelled
	}
	if predicates.IsCompleted(task) {
		return IOSCategoryTaskCompleted
	}
	if predicates.IsInProgress(task) {
		return IOSCategoryTaskInProgress
	}
	return IOSCategoryTaskActionable
}

// GetTaskNotificationTitle returns the notification title for a task notification type
func GetTaskNotificationTitle(notificationType models.NotificationType) string {
	switch notificationType {
	case models.NotificationTaskDueSoon:
		return "Task Due Soon"
	case models.NotificationTaskOverdue:
		return "Task Overdue"
	case models.NotificationTaskCompleted:
		return "Task Completed"
	case models.NotificationTaskAssigned:
		return "Task Assigned"
	default:
		return "Task Update"
	}
}
