import type { Database, Tables, TablesInsert, TablesUpdate } from './database.types'

export type { Database, Tables, TablesInsert, TablesUpdate, Enums, CompositeTypes, Json } from './database.types'

export { Constants } from './database.types'

export type SupabaseClient = import('@supabase/supabase-js').SupabaseClient<Database>

// Note: supabaseClient is a server-only export available via @repo/core/server
// DO NOT export it from the main entry point to prevent browser bundling

export type SupabaseUserProfile = Tables<'users'>
export type SupabaseOrganization = Tables<'organizations'>
export type SupabaseOrgMembership = Tables<'org_memberships'>
export type SupabaseTaskSchedule = Tables<'task_schedules'>
export type SupabaseCommandQueue = Tables<'command_queue'>
export type SupabaseExecutionLogs = Tables<'execution_logs'>
export type SupabaseApiKey = Tables<'api_keys'>
/** API response type for API key list items (omits sensitive key_hash) */
export type ApiKeyListItem = Omit<SupabaseApiKey, 'key_hash'>
export type SupabaseOrganizationCredential = Tables<'credentials'>

export type SupabaseUserProfileInsert = TablesInsert<'users'>
export type SupabaseOrganizationInsert = TablesInsert<'organizations'>
export type SupabaseTaskScheduleInsert = TablesInsert<'task_schedules'>
export type SupabaseCommandQueueInsert = TablesInsert<'command_queue'>
export type SupabaseApiKeyInsert = TablesInsert<'api_keys'>
export type SupabaseExecutionLogsInsert = TablesInsert<'execution_logs'>
export type SupabaseOrganizationCredentialInsert = TablesInsert<'credentials'>

export type SupabaseUserProfileUpdate = TablesUpdate<'users'>
export type SupabaseOrganizationUpdate = TablesUpdate<'organizations'>
export type SupabaseTaskScheduleUpdate = TablesUpdate<'task_schedules'>
export type SupabaseCommandQueueUpdate = TablesUpdate<'command_queue'>
export type SupabaseApiKeyUpdate = TablesUpdate<'api_keys'>
export type SupabaseExecutionLogsUpdate = TablesUpdate<'execution_logs'>
export type SupabaseOrganizationCredentialUpdate = TablesUpdate<'credentials'>

// Export helper types and utilities
export type { UpsertUserProfileResult } from './helpers'
export { sanitizePostgrestValue, eqOrgId } from './helpers'

// Composite types for joined queries
export type SupabaseMembershipWithOrganization = SupabaseOrgMembership & {
  organization: SupabaseOrganization | null
}
