package inventory import ( "errors" "fmt" "net/url" "strings" "time" ) // Price is one of an entry's plans, as the user read it on their site: // what it costs, per what, when it was checked and where. Pages that // compare with a competitor say only these prices, with the date. type Price struct { Plan string `json:"plan,omitempty" yaml:"plan,omitempty"` // "Standard", "Pro"… Amount string `json:"amount" yaml:"amount"` // as written: "$25", "€19", "Free" Per string `json:"per,omitempty" yaml:"per,omitempty"` // "per agent per month", "per year" Checked string `json:"checked" yaml:"checked"` // YYYY-MM-DD Source string `json:"source,omitempty" yaml:"source,omitempty"` // the pricing page } // StaleAfter is how old a price can be before pages built with it say to // check it again. const StaleAfter = 180 * 24 * time.Hour const dateFormat = "2006-01-02" // normalizePrices drops empty rows and checks the rest. func (e *Entry) normalizePrices() error { var keep []Price for _, p := range e.Prices { p.Plan, p.Amount, p.Per = strings.TrimSpace(p.Plan), strings.TrimSpace(p.Amount), strings.TrimSpace(p.Per) p.Checked, p.Source = strings.TrimSpace(p.Checked), strings.TrimSpace(p.Source) if p.Amount == "" { if p.Plan != "" || p.Per != "" || p.Source != "" { return fmt.Errorf("%s: give the price of %q, or clear its row", e.Name, or(p.Plan, "the plan")) } continue } if p.Checked == "" { return fmt.Errorf("%s: say when you checked the price %s", e.Name, p.Amount) } if _, err := time.Parse(dateFormat, p.Checked); err != nil { return fmt.Errorf("%s: %q isn't a date (YYYY-MM-DD)", e.Name, p.Checked) } if p.Source != "" { if u, err := url.Parse(p.Source); err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { return errors.New(e.Name + ": the price's source must be a web address (https://…)") } } keep = append(keep, p) } e.Prices = keep return nil } // String is a price in words: "Pro: $25 per agent per month". func (p Price) String() string { s := p.Amount if p.Per != "" { s += " " + p.Per } if p.Plan != "" { s = p.Plan + ": " + s } return s } // CheckedOn is when the price was checked, as "24 Nov 2026". func (p Price) CheckedOn() string { t, err := time.Parse(dateFormat, p.Checked) if err != nil { return p.Checked } return t.Format("2 Jan 2006") } // Stale says whether a price is older than StaleAfter. func (p Price) Stale(now time.Time) bool { t, err := time.Parse(dateFormat, p.Checked) return err != nil || now.Sub(t) > StaleAfter } // StalePrices are the entry's prices due for another look. func (e *Entry) StalePrices(now time.Time) []Price { var out []Price for _, p := range e.Prices { if p.Stale(now) { out = append(out, p) } } return out } // PricesPrompt is the entry's prices for the AI, each with when and where // it was read; "" without prices. func (e *Entry) PricesPrompt() string { if len(e.Prices) == 0 { return "" } var b strings.Builder fmt.Fprintf(&b, "%s's prices, as the owner read them on %s's site (the only prices you may state for them, each with the month it was checked, e.g. \"as of Nov 2026\"):\n", e.Name, e.Name) for _, p := range e.Prices { t, _ := time.Parse(dateFormat, p.Checked) fmt.Fprintf(&b, "- %s (checked %s", p.String(), t.Format("Jan 2006")) if p.Source != "" { fmt.Fprintf(&b, ", from %s", p.Source) } b.WriteString(")\n") } return b.String() } func or(a, b string) string { if a != "" { return a } return b }