/**
 * Supabase Invitation Types
 * Generated from database schema with type aliases for consistent naming
 */

import type { Database } from '../../../supabase/database.types'

// Type aliases for org_invitations table
export type SupabaseOrgInvitation = Database['public']['Tables']['org_invitations']['Row']
export type SupabaseOrgInvitationInsert = Database['public']['Tables']['org_invitations']['Insert']
export type SupabaseOrgInvitationUpdate = Database['public']['Tables']['org_invitations']['Update']

// Extended type with computed fields for UI
export interface AdminOrgInvitation extends SupabaseOrgInvitation {
  organizations?: {
    id: string
    name: string
    status?: string
  }
  users?: {
    id: string
    email: string
    first_name?: string | null
    last_name?: string | null
    profile_picture_url?: string | null
  }
}

// Invitation states from database constraint
export type InvitationState = 'pending' | 'accepted' | 'revoked' | 'expired'

// Helper function to transform Supabase data to internal format
export function transformSupabaseToInvitation(supabaseInvitation: SupabaseOrgInvitation) {
  return {
    id: supabaseInvitation.id,
    workosInvitationId: supabaseInvitation.workos_invitation_id,
    email: supabaseInvitation.email,
    organizationId: supabaseInvitation.organization_id,
    inviterUserId: supabaseInvitation.inviter_user_id,
    invitationState: supabaseInvitation.invitation_state as InvitationState | null,
    roleSlug: supabaseInvitation.role_slug,
    invitationToken: supabaseInvitation.invitation_token,
    acceptInvitationUrl: supabaseInvitation.accept_invitation_url,
    acceptedAt: supabaseInvitation.accepted_at,
    revokedAt: supabaseInvitation.revoked_at,
    expiresAt: supabaseInvitation.expires_at,
    createdAt: supabaseInvitation.created_at,
    updatedAt: supabaseInvitation.updated_at
  }
}
