package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	"llm_processor/processor"
	"llm_processor/storage"

	"github.com/spf13/cobra"
)

var (
	inputFile      string
	outputFile     string
	criteriaFile   string
	modelName      string
	batchSize      int
	delaySeconds   int
	timeoutSeconds int
)

var rootCmd = &cobra.Command{
	Use:   "llm-processor",
	Short: "Process papers using LLM with configurable criteria",
	Long: `A command line tool for processing academic papers through
language models using customizable evaluation criteria.`,
	Run: func(cmd *cobra.Command, args []string) {
		// Get OpenRouter API key from environment
		apiKey := os.Getenv("OPENROUTER_API_KEY")
		if apiKey == "" {
			log.Fatal("OPENROUTER_API_KEY environment variable is required")
		}

		// Load papers
		papers, err := storage.LoadPapers(inputFile)
		if err != nil {
			log.Fatalf("Failed to load papers: %v", err)
		}

		// Initialize processor
		proc := processor.NewProcessor(modelName, batchSize, apiKey)
		proc.SetTimeout(time.Duration(timeoutSeconds) * time.Second)

		// Process papers
		ctx := context.Background()
		if err := proc.ProcessPapers(ctx, inputFile, outputFile, criteriaFile, time.Duration(delaySeconds)*time.Second); err != nil {
			log.Fatalf("Failed to save results: %v", err)
		}

		fmt.Printf("Successfully processed %d papers\n", len(papers))
	},
}

func init() {
	rootCmd.PersistentFlags().StringVarP(&inputFile, "input", "i", "", "Input JSON file containing papers")
	rootCmd.PersistentFlags().StringVarP(&outputFile, "output", "o", "results.json", "Output JSON file for results")
	rootCmd.PersistentFlags().StringVarP(&criteriaFile, "criteria", "c", "", "Text file containing evaluation criteria")
	rootCmd.PersistentFlags().StringVarP(&modelName, "model", "m", "gpt-4", "LLM model to use")
	rootCmd.PersistentFlags().IntVarP(&batchSize, "batch-size", "b", 32, "Batch size for processing")
	rootCmd.PersistentFlags().IntVarP(&delaySeconds, "delay", "d", 0, "Delay in seconds between paper submissions")
	rootCmd.PersistentFlags().IntVarP(&timeoutSeconds, "timeout", "t", 3600, "Timeout in seconds for processing (default 1h)")

	// Mark required flags
	rootCmd.MarkPersistentFlagRequired("input")
	rootCmd.MarkPersistentFlagRequired("criteria")
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
