// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

// Package completion provides shell completion helpers for CLI commands.
package completion

import (
	"git.secluded.site/go-lunatask"
	"git.secluded.site/lune/internal/config"
	"github.com/spf13/cobra"
)

// Areas returns area keys from config for shell completion.
func Areas(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	cfg, err := config.Load()
	if err != nil {
		return nil, cobra.ShellCompDirectiveError
	}

	keys := make([]string, len(cfg.Areas))
	for i, a := range cfg.Areas {
		keys[i] = a.Key
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Goals returns all goal keys across all areas for shell completion.
func Goals(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	cfg, err := config.Load()
	if err != nil {
		return nil, cobra.ShellCompDirectiveError
	}

	totalGoals := 0
	for _, a := range cfg.Areas {
		totalGoals += len(a.Goals)
	}

	keys := make([]string, 0, totalGoals)

	for _, a := range cfg.Areas {
		for _, g := range a.Goals {
			keys = append(keys, g.Key)
		}
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Notebooks returns notebook keys from config for shell completion.
func Notebooks(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	cfg, err := config.Load()
	if err != nil {
		return nil, cobra.ShellCompDirectiveError
	}

	keys := make([]string, len(cfg.Notebooks))
	for i, n := range cfg.Notebooks {
		keys[i] = n.Key
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Habits returns habit keys from config for shell completion.
func Habits(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	cfg, err := config.Load()
	if err != nil {
		return nil, cobra.ShellCompDirectiveError
	}

	keys := make([]string, len(cfg.Habits))
	for i, h := range cfg.Habits {
		keys[i] = h.Key
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Relationships returns relationship strength options for shell completion.
func Relationships(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	rs := lunatask.AllRelationshipStrengths()
	keys := make([]string, len(rs))

	for i, r := range rs {
		keys[i] = string(r)
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Workflows returns workflow options for shell completion.
func Workflows(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	workflows := lunatask.Workflows()
	keys := make([]string, len(workflows))

	for i, w := range workflows {
		keys[i] = string(w)
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// TaskStatuses returns task status options for shell completion.
func TaskStatuses(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	statuses := lunatask.AllTaskStatuses()
	keys := make([]string, len(statuses))

	for i, s := range statuses {
		keys[i] = string(s)
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Priorities returns priority options for shell completion.
func Priorities(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	priorities := lunatask.AllPriorities()
	keys := make([]string, len(priorities))

	for i, p := range priorities {
		keys[i] = p.String()
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}

// Motivations returns motivation options for shell completion.
func Motivations(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
	motivations := lunatask.AllMotivations()
	keys := make([]string, len(motivations))

	for i, m := range motivations {
		keys[i] = string(m)
	}

	return keys, cobra.ShellCompDirectiveNoFileComp
}
