package storage

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"arxiv-processor/arxiv"
)

// JSONStorage handles saving papers to JSON files
type JSONStorage struct {
	FilePath string
}

// NewJSONStorage creates a new JSON storage handler
func NewJSONStorage(filePath string) *JSONStorage {
	return &JSONStorage{FilePath: filePath}
}

// SavePapers saves papers to a JSON file
func (s *JSONStorage) SavePapers(papers []arxiv.Paper) error {
	file, err := os.Create(s.FilePath)
	if err != nil {
		return fmt.Errorf("failed to create file: %w", err)
	}
	defer file.Close()

	encoder := json.NewEncoder(file)
	encoder.SetIndent("", "  ")
	if err := encoder.Encode(papers); err != nil {
		return fmt.Errorf("failed to encode papers: %w", err)
	}

	return nil
}

// Paper represents a simplified paper structure for JSON storage
type Paper struct {
	ID        string    `json:"id"`
	Title     string    `json:"title"`
	Published time.Time `json:"published"`
	Authors   []string  `json:"authors"`
}
