package model

import (
	"net/url"
	"time"
)

// TODO docstring
type Article struct {
	Identifier  int
	SourceUrl   string
	PublishDate time.Time
	FetchDate   time.Time
	Title       string
	Content     string
	AiSummary   string
}

// TODO docstring
type ArticleViewModel struct {
	Title        string
	PublishDate  string
	SourceUrl    string
	ShortSource  string
	Summary      string
	AiSummarized bool
}

// TODO docstring
func (a *Article) ViewModel() *ArticleViewModel {
	summary := a.AiSummary
	if summary == "" {
		if len(a.Content) > 200 {
			summary = a.Content[:200]
		} else {
			summary = a.Content
		}
	}

	short_url := ""
	parsedURL, err := url.Parse(a.SourceUrl)
	if err == nil {
		short_url = parsedURL.Hostname()
	}

	return &ArticleViewModel{
		Title:        a.Title,
		PublishDate:  a.PublishDate.Local().Format("02.01.2006"),
		SourceUrl:    a.SourceUrl,
		ShortSource:  short_url,
		Summary:      summary,
		AiSummarized: a.AiSummary != "",
	}
}
