package feeds

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

// Feed represents an RSS/Atom feed
type Feed struct {
	ID          string    `json:"id" gorm:"primaryKey"`
	Title       string    `json:"title"`
	URL         string    `json:"url" gorm:"uniqueIndex"`
	Description string    `json:"description"`
	SiteURL     string    `json:"siteUrl"`
	ImageURL    string    `json:"imageUrl"`
	LastFetched time.Time `json:"lastFetched"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
	Entries     []Entry   `json:"entries,omitempty" gorm:"foreignKey:FeedID"`
}

// Entry represents a single item/entry in a feed
type Entry struct {
	ID          string     `json:"id" gorm:"primaryKey"`
	FeedID      string     `json:"feedId" gorm:"index"`
	Title       string     `json:"title"`
	URL         string     `json:"url" gorm:"uniqueIndex"`
	Content     string     `json:"content"`
	Summary     string     `json:"summary"`
	Author      string     `json:"author"`
	Published   time.Time  `json:"published"`
	Updated     time.Time  `json:"updated"`
	ReadAt      *time.Time `json:"readAt"`
	FullContent string     `json:"fullContent"`
	CreatedAt   time.Time  `json:"createdAt"`
	UpdatedAt   time.Time  `json:"updatedAt"`
}

// BeforeCreate is a GORM hook that generates a UUID for new feeds
func (f *Feed) BeforeCreate(tx *gorm.DB) error {
	if f.ID == "" {
		f.ID = uuid.New().String()
	}
	return nil
}

// BeforeCreate is a GORM hook that generates a UUID for new entries
func (e *Entry) BeforeCreate(tx *gorm.DB) error {
	if e.ID == "" {
		e.ID = uuid.New().String()
	}
	return nil
}