package cmd

import (
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"log"

	"github.com/Jaculabilis/intake/core"
	_ "github.com/mattn/go-sqlite3"
	"github.com/spf13/cobra"
)

var itemAddCmd = &cobra.Command{
	Use:   "add",
	Short: "Add an item",
	Long: `Create an ad-hoc item in a source.

By default, the item is created in the "default" source, which is created
if it doesn't exist, with a random id.`,
	Run: func(cmd *cobra.Command, args []string) {
		itemAdd()
	},
}

var addItemSource string
var addItemId string
var addItemTitle string
var addItemAuthor string
var addItemBody string
var addItemLink string
var addItemTime int
var addItemActions string

func init() {
	itemCmd.AddCommand(itemAddCmd)

	itemAddCmd.Flags().StringVarP(&addItemSource, "source", "s", "", "Source in which to create the item (default: default)")
	itemAddCmd.Flags().StringVarP(&addItemId, "id", "i", "", "Item id (default: random hex)")
	itemAddCmd.Flags().StringVarP(&addItemTitle, "title", "t", "", "Item title")
	itemAddCmd.Flags().StringVarP(&addItemAuthor, "author", "a", "", "Item author")
	itemAddCmd.Flags().StringVarP(&addItemBody, "body", "b", "", "Item body")
	itemAddCmd.Flags().StringVarP(&addItemLink, "link", "l", "", "Item link")
	itemAddCmd.Flags().IntVarP(&addItemTime, "time", "m", 0, "Item time as a Unix timestamp")
	itemAddCmd.Flags().StringVarP(&addItemActions, "action", "x", "", "Item time as a Unix timestamp")
}

func itemAdd() {
	// Default to "default" source
	if addItemSource == "" {
		addItemSource = "default"
	}
	// Default id to random hex string
	if addItemId == "" {
		bytes := make([]byte, 16)
		if _, err := rand.Read(bytes); err != nil {
			log.Fatalf("error: failed to generate id: %v", err)
		}
		addItemId = hex.EncodeToString(bytes)
	}

	var actions core.Actions
	if addItemActions != "" {
		if err := json.Unmarshal([]byte(addItemActions), &actions); err != nil {
			log.Fatalf("error: could not parse actions: %v", err)
		}
	}

	db := openAndMigrateDb()

	if err := core.AddItems(db, []core.Item{{
		Source: addItemSource,
		Id:     addItemId,
		Title:  addItemTitle,
		Author: addItemAuthor,
		Body:   addItemBody,
		Link:   addItemLink,
		Time:   addItemTime,
		Action: actions,
	}}); err != nil {
		log.Fatalf("error: failed to add item: %s", err)
	}

	log.Printf("Added %s/%s\n", addItemSource, addItemId)
}