package repository import ( "database/sql" ) // FactParam is one generic fact to persist (V3 S2, migration 000019). // ValueNum/ValueDate nil when the type carries no numeric/date value. type FactParam struct { Type string ValueText string ValueNum *float64 ValueDate *string // ISO YYYY-MM-DD Confidence float64 } // FactRow is one persisted fact (auto-extracted or user-corrected). type FactRow struct { ID string Type string ValueText string ValueNum *float64 ValueDate *string Confidence float64 Origin string } // UpsertAutoFacts replaces the origin='auto' facts of a resource with a fresh // extraction. Rows with origin='user' (S3 corrections) are never touched. // An empty params clears stale auto facts (e.g. OCR re-run yielding nothing). func (r *Resources) UpsertAutoFacts(resourceID string, params []FactParam) error { tx, err := r.DB.Begin() if err != nil { return err } defer tx.Rollback() //nolint:errcheck — no-op after Commit. if _, err := tx.Exec( `DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'auto'`, resourceID, ); err != nil { return err } for _, p := range params { id, err := NewIDErr() if err != nil { return err } if _, err := tx.Exec( `INSERT INTO resource_facts (id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin) VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'auto')`, id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence), ); err != nil { return err } } return tx.Commit() } // ListFacts returns the facts of a resource, user-corrected first then auto // (stable order by type). No visibility check — the caller enforces viewer+ // (GetFileVisible) like GetFileOcr. func (r *Resources) ListFacts(resourceID string) ([]FactRow, error) { rows, err := r.DB.Query( `SELECT id, fact_type, COALESCE(value_text, ''), value_num, COALESCE(value_date::text, ''), confidence, origin FROM resource_facts WHERE resource_id = $1 ORDER BY CASE origin WHEN 'user' THEN 0 ELSE 1 END, fact_type, value_text`, resourceID, ) if err != nil { return nil, err } defer rows.Close() out := make([]FactRow, 0) for rows.Next() { var row FactRow var num sql.NullFloat64 var dat string if err := rows.Scan(&row.ID, &row.Type, &row.ValueText, &num, &dat, &row.Confidence, &row.Origin); err != nil { return nil, err } if num.Valid { v := num.Float64 row.ValueNum = &v } if dat != "" { d := dat row.ValueDate = &d } out = append(out, row) } return out, rows.Err() } // factNeedingExtract is a file with OCR text but no auto facts yet. type factNeedingExtract struct { ResourceID string Text string } // ListNeedingExtract returns up to limit files with non-empty ocr_text and no // origin='auto' facts (boot backfill, S2). Live rows only. func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error) { rows, err := r.DB.Query( `SELECT resource_id, ocr_text FROM resources WHERE type = 'file' AND deleted_at IS NULL AND ocr_text IS NOT NULL AND ocr_text <> '' AND NOT EXISTS ( SELECT 1 FROM resource_facts WHERE resource_facts.resource_id = resources.resource_id AND origin = 'auto') ORDER BY updated_at ASC LIMIT $1`, limit, ) if err != nil { return nil, err } defer rows.Close() var out []factNeedingExtract for rows.Next() { var row factNeedingExtract if err := rows.Scan(&row.ResourceID, &row.Text); err != nil { return nil, err } out = append(out, row) } return out, rows.Err() } func nullableFloat(v *float64) any { if v == nil { return nil } return *v } func derefStr(v *string) string { if v == nil { return "" } return *v } func clamp01(v float64) float64 { if v < 0 { return 0 } if v > 1 { return 1 } return v }