package inbox

import (
	"fmt"

	"ai-workflow-skill/internal/protocol"

	"github.com/spf13/cobra"
)

func newInitCmd(opts *rootOptions) *cobra.Command {
	return &cobra.Command{
		Use:   "init",
		Short: "Initialize the shared SQLite database schema",
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			sqlDB, err := openInboxDB(ctx, opts.dbPath)
			if err != nil {
				return err
			}
			defer sqlDB.Close()

			resp := protocol.Success{
				OK:      true,
				Command: "init",
				Data: map[string]any{
					"db_path": opts.dbPath,
					"status":  "initialized",
				},
			}

			if opts.json {
				return protocol.WriteJSON(cmd.OutOrStdout(), resp)
			}

			_, err = fmt.Fprintf(cmd.OutOrStdout(), "initialized database: %s\n", opts.dbPath)
			return err
		},
	}
}
