package repository import ( "context" "fmt" "github.com/geo-platform/tenant-api/internal/tenant/prompts" "github.com/geo-platform/tenant-api/internal/tenant/repository/generated" ) func EnsurePlatformTemplates(ctx context.Context, db generated.DBTX) (int, error) { seeds := prompts.PlatformTemplateSeeds() for _, tpl := range seeds { _, err := db.Exec(ctx, ` INSERT INTO article_templates ( scope, tenant_id, origin_type, template_key, template_name, prompt_template, prompt_visibility, card_config_json, status, version_no ) VALUES ('platform', NULL, 'platform', $1, $2, $3, 'visible', $4::jsonb, 'active', 1) ON CONFLICT (scope, template_key, version_no) WHERE deleted_at IS NULL AND tenant_id IS NULL DO UPDATE SET template_name = EXCLUDED.template_name, prompt_template = EXCLUDED.prompt_template, card_config_json = EXCLUDED.card_config_json, prompt_visibility = 'visible', status = 'active', updated_at = NOW() `, tpl.Key, tpl.Name, tpl.PromptTemplate, tpl.CardConfigJSON) if err != nil { return 0, fmt.Errorf("upsert platform template %q: %w", tpl.Key, err) } } return len(seeds), nil }