// Ingress apps to smoke-test. Each entry = NAME-APP.<BASE_DOMAIN>.
// `ready` is a selector or text that only appears once the app actually rendered
// (not just a 200 from nginx's default backend).
//
// EDIT THIS LIST to match what you actually expose. Confirmed hosts are marked;
// others are best-guess — fix the subdomain if yours differs.

export type Target = {
  name: string;
  subdomain: string;
  path?: string;
  // A locator that proves the app's own UI rendered.
  ready: { role?: string; name?: RegExp; text?: RegExp; selector?: string };
  // Skip in the default reachability run (has a dedicated spec, e.g. auth flow).
  dedicated?: boolean;
};

export const BASE_DOMAIN = process.env.BASE_DOMAIN ?? 'riotpiao.com';

export function url(t: Pick<Target, 'subdomain' | 'path'>): string {
  return `https://${t.subdomain}.${BASE_DOMAIN}${t.path ?? '/'}`;
}

// Apps that federate login through Authentik (OIDC/OAuth2). The OAuth spec drives
// the full chain: open the app, click its "sign in via SSO" control, complete the
// Authentik login on redirect, and assert we land back in the app authenticated.
// EDIT selectors to match your apps — the SSO button text/label varies per app.
export type OAuthTarget = {
  name: string;
  subdomain: string;
  path?: string;
  // The app's "log in with Authentik/SSO" control.
  ssoButton: { role?: string; name?: RegExp; selector?: string };
  // Proof we are authenticated back inside the app after the round-trip.
  success: { urlRe?: RegExp; text?: RegExp; selector?: string };
};

export const OAUTH_TARGETS: OAuthTarget[] = [
  {
    name: 'argocd',
    subdomain: 'argocd',
    ssoButton: { role: 'link', name: /log ?in via|authentik|sso|oidc/i },
    success: { urlRe: /\/applications/i, text: /Applications/i },
  },
  {
    name: 'forgejo',
    subdomain: 'forgejo',
    path: '/user/login',
    ssoButton: { role: 'link', name: /authentik|oauth|sign in with|openid/i },
    success: { selector: 'a[href="/notifications"], .avatar, nav .user' },
  },
  {
    name: 'grafana',
    subdomain: 'grafana',
    path: '/login',
    ssoButton: { role: 'link', name: /sign in with|authentik|oauth/i },
    success: { urlRe: /\/(\?|$)|\/d\//, text: /Welcome to Grafana|Dashboards|Home/i },
  },
];

export const TARGETS: Target[] = [
  // confirmed hosts (from bootstrap values)
  { name: 'argocd', subdomain: 'argocd', ready: { text: /Argo\s*CD|Let's get started|Login/i } },
  { name: 'forgejo', subdomain: 'forgejo', ready: { text: /Forgejo|Sign In|Explore/i } },

  // dashboards / apps — adjust subdomain to your actual ingress host
  { name: 'portainer', subdomain: 'portainer', dedicated: true, ready: { selector: 'input[type="password"]' } },
  { name: 'authentik', subdomain: 'authentik', dedicated: true, ready: { selector: 'input[name="uidField"], input[type="password"]' } },
  { name: 'homarr', subdomain: 'homarr', ready: { text: /Homarr|Dashboard/i } },
  { name: 'grafana', subdomain: 'grafana', ready: { text: /Grafana|Welcome/i } },
  { name: 'minio', subdomain: 'minio', ready: { selector: 'input, button' } },
];
