// 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/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) {
	return []string{
		"family",
		"intimate-friends",
		"close-friends",
		"casual-friends",
		"acquaintances",
		"business-contacts",
		"almost-strangers",
	}, cobra.ShellCompDirectiveNoFileComp
}
