package store

import (
	"context"
)

type RouteProbeTarget struct {
	Candidate RuntimeModelCandidate
	Hot       bool
}

func (s *Store) ListRouteProbeTargets(ctx context.Context) ([]RouteProbeTarget, error) {
	rows, err := s.pool.Query(ctx, `
SELECT platform.id::text,
       platform.provider,
       COALESCE(NULLIF(platform.config->>'specType', ''), NULLIF(catalog.provider_type, ''), NULLIF(platform.config->>'sourceSpecType', ''), platform.provider),
       COALESCE(
         NULLIF(platform.base_url, ''),
         NULLIF(platform.config->>'endpoint', ''),
         NULLIF(platform.config->>'baseURL', ''),
         NULLIF(platform.config->>'base_url', ''),
         NULLIF(catalog.default_base_url, ''),
         ''
       ),
       platform.config,
       model.id::text,
       COALESCE(model.model_type->>0, ''),
       EXISTS (
         SELECT 1
         FROM gateway_task_attempts attempt
         WHERE attempt.platform_model_id = model.id
           AND attempt.started_at > now() - interval '10 minutes'
       )
FROM integration_platforms platform
JOIN platform_models model ON model.platform_id = platform.id
LEFT JOIN model_catalog_providers catalog
  ON catalog.provider_key = platform.provider OR catalog.provider_code = platform.provider
WHERE platform.status = 'enabled'
  AND platform.deleted_at IS NULL
  AND model.enabled = true
  AND COALESCE(
        NULLIF(platform.base_url, ''),
        NULLIF(platform.config->>'endpoint', ''),
        NULLIF(platform.config->>'baseURL', ''),
        NULLIF(platform.config->>'base_url', ''),
        NULLIF(catalog.default_base_url, '')
      ) IS NOT NULL
ORDER BY platform.id, model.id`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := make([]RouteProbeTarget, 0)
	for rows.Next() {
		var item RouteProbeTarget
		var config []byte
		if err := rows.Scan(
			&item.Candidate.PlatformID, &item.Candidate.Provider, &item.Candidate.SpecType,
			&item.Candidate.BaseURL, &config, &item.Candidate.PlatformModelID,
			&item.Candidate.ModelType, &item.Hot,
		); err != nil {
			return nil, err
		}
		item.Candidate.PlatformConfig = decodeObject(config)
		items = append(items, item)
	}
	return items, rows.Err()
}
