/*
|--------------------------------------------------------------------------
| Agência PSI
|--------------------------------------------------------------------------
| Arquivo: src/features/tenantship/services/tenantMembersSelects.js
|
| SELECTs canônicos pra tenant_members + view v_tenant_members_with_profiles.
|--------------------------------------------------------------------------
*/

/**
 * SELECT direto da tabela tenant_members (sem join).
 * Use quando precisa só dos campos crus.
 */
export const TENANT_MEMBER_RAW_SELECT = `
  id, tenant_id, user_id, role, status, created_at
`.trim();

/**
 * SELECT enriched via view v_tenant_members_with_profiles.
 * Inclui full_name e email (joins com profiles e auth.users).
 * Use pra listagens da UI.
 */
export const TENANT_MEMBER_PROFILE_SELECT = `
  tenant_member_id, tenant_id, user_id, role, status, created_at,
  full_name, email
`.trim();

/**
 * Normaliza row da view pra usar `id` no lugar de `tenant_member_id`.
 */
export function flattenMemberRow(r) {
    if (!r) return r;
    const out = { ...r };
    if (r.tenant_member_id && !r.id) out.id = r.tenant_member_id;
    return out;
}
