/*
|--------------------------------------------------------------------------
| Agência PSI
|--------------------------------------------------------------------------
| Arquivo: src/features/tenantship/services/tenantInvitesSelects.js
|
| Fonte única do SELECT de tenant_invites.
|--------------------------------------------------------------------------
*/

export const TENANT_INVITE_SELECT = `
  id, tenant_id, email, role, token,
  invited_by, created_at, expires_at,
  accepted_at, accepted_by,
  revoked_at, revoked_by
`.trim();

/**
 * Computa status derivado do invite (sem campo no banco — calculado em runtime).
 *
 * @param {Object} row
 * @returns {'pending'|'expired'|'accepted'|'revoked'}
 */
export function deriveInviteStatus(row) {
    if (!row) return 'pending';
    if (row.revoked_at) return 'revoked';
    if (row.accepted_at) return 'accepted';
    if (row.expires_at && new Date(row.expires_at) < new Date()) return 'expired';
    return 'pending';
}

/**
 * Achata a row adicionando o status derivado.
 */
export function flattenInviteRow(r) {
    if (!r) return r;
    return { ...r, status: deriveInviteStatus(r) };
}
