import type { SupabaseClient } from '@supabase/supabase-js'

/**
 * Currently the time-tracking app is scoped to a single org: Bowden Works.
 * Adi belongs to both Bowden Works and Tingang, but the bridge data lives
 * in Bowden Works. RLS still gates access; if the user isn't a Bowden Works
 * member they'll just see no rows.
 */
export const APP_ORG_SLUG = 'bowden-works'

export type AppOrg = {
  id: string
  name: string
  default_hourly_rate_usd: number
}

/** Fallback if the column hasn't been set (shouldn't happen post-0007). */
export const FALLBACK_BASE_RATE = 14

export async function getAppOrg(
  supabase: SupabaseClient,
): Promise<AppOrg | null> {
  const { data, error } = await supabase
    .from('organizations')
    .select('id, name, default_hourly_rate_usd')
    .eq('slug', APP_ORG_SLUG)
    .maybeSingle()
  if (error || !data) return null
  return {
    id: data.id,
    name: data.name,
    default_hourly_rate_usd:
      Number(data.default_hourly_rate_usd ?? FALLBACK_BASE_RATE),
  }
}
