All files / src/services team-service.ts

55.84% Statements 444/795
46.06% Branches 41/89
64.28% Functions 9/14
55.84% Lines 444/795

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 7951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 7x 7x 7x 7x 7x 7x     7x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 2x 2x 2x 3x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               3x                                               3x                       3x                                                             3x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 3x 3x 3x 4x 1x 1x 2x 4x                   2x 2x 2x 2x 2x 4x     2x 2x 2x 2x 2x 2x 4x     2x 2x 2x 2x 2x 2x 2x 2x 2x 4x     2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x     1x 1x 1x 1x 4x     1x 1x 4x     4x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x     1x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x                                                                         1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 3x 1x 3x 1x 3x 3x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x             1x 1x 1x 1x                                                                                     1x 1x 1x                                                                                     1x 1x 1x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x     2x 1x 1x 1x 1x 1x 1x 1x 2x     1x 1x 1x 1x 2x     1x 1x 2x     2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x                                                                                                                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 1x     1x
import * as vscode from 'vscode';
import { getAuthContext } from './auth-service';
import { insertParticipantStatusEvent } from './team-activity-service';
import { ProjectInfo, getCurrentProjectInfo, validateCurrentProject, getProjectDescription } from './project-detection-service';
import { verifyGitHubPushAccess, isGitHubRepository, promptGitHubVerification } from './github-verification-service';
 
// Team-related types
export interface Team {
    id: string;
    lobby_name: string;
    created_by: string;
    join_code: string;
    created_at: string;
    project_repo_url?: string;
    project_identifier: string;
    project_name?: string;
 
    // GitHub verification fields
    verified?: boolean;
    verification_method?: string;
    verified_by?: string;
    verified_at?: string;
    github_repo_id?: string;
}
 
export interface TeamWithMembership extends Team {
    role: 'member' | 'admin';
}
 
 
//Generates a random 6-character alphanumeric team join code
 
function generateJoinCode(): string {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let result = '';
    for (let i = 0; i < 6; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}
 
 
//Gets the shared Supabase client instance for database operations (uses current user session)
async function getSupabaseClient() {
    try {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { getSupabase } = require('../auth/supabaseClient');
        return getSupabase();
    } catch (error) {
        throw new Error(`Failed to initialize Supabase client: ${error}`);
    }
}
 
 
//Creates a new team with the authenticated user as admin, automatically linking it to the current project
 
export async function createTeam(lobbyName: string): Promise<{ team?: Team; joinCode?: string; error?: string }> {
    try {
        // Check authentication
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { error: 'User must be authenticated to create a team' };
        }
 
        // Get current project information
        const currentProject = getCurrentProjectInfo();
        if (!currentProject) {
            return { error: 'No workspace folder is open. Please open a project folder to create a team.' };
        }
 
        // Enforce Git requirement for team functionality
        if (!currentProject.isGitRepo || !currentProject.remoteUrl) {
            return { 
                error: 'Team functionality requires a Git repository with a remote origin.\n\n' +
                       'To create a team:\n' +
                       '1. Initialize Git: git init\n' +
                       '2. Add remote origin: git remote add origin <repository-url>\n' +
                       '3. Push your code to the remote repository\n\n' +
                       'All team members must have the same Git repository cloned locally.'
            };
        }

        const supabase = await getSupabaseClient();

        // Optional GitHub verification - silently checks if user has a token
        // If they do and have access, great! If not, no problem - just create unverified team
        let verificationData: {
            verified: boolean;
            verification_method: string;
            github_repo_id?: string;
        } = {
            verified: false,
            verification_method: 'none'
        };

        // Try to verify silently (doesn't block team creation if it fails)
        if (currentProject.remoteUrl && isGitHubRepository(currentProject.remoteUrl)) {
            try {
                const verificationResult = await verifyGitHubPushAccess(currentProject.remoteUrl);

                if (verificationResult.hasAccess) {
                    // User has a token AND has access - mark as verified
                    verificationData = {
                        verified: true,
                        verification_method: 'github_token',
                        github_repo_id: verificationResult.repoInfo?.repoId.toString()
                    };
                    console.log('[Team Creation] ✓ Repository verified with GitHub token');
                } else {
                    // No token or no access - that's fine, just create unverified
                    console.log('[Team Creation] Creating unverified team (no token or no access)');
                }
            } catch (error) {
                // Any error - just create unverified team
                console.log('[Team Creation] Verification skipped, creating unverified team');
            }
        }

        // Get current user's auth ID for verified_by field
        const { data: { user: authUser }, error: authUserError } = await supabase.auth.getUser();
        if (authUserError || !authUser) {
            return { error: 'Failed to get current user for verification record.' };
        }

        // Use secure RPC which creates the team and adds the creator as admin
        const { data: teamData, error: rpcError } = await supabase.rpc('create_team', {
            p_lobby_name: lobbyName,
            p_project_name: currentProject.projectName,
            p_project_identifier: currentProject.projectHash,
            p_project_repo_url: currentProject.remoteUrl
        }).single();

        if (rpcError || !teamData) {
            // Check for duplicate team name error
            if (rpcError?.message?.includes('DUPLICATE_TEAM_NAME')) {
                return { error: 'A team with this name already exists. Please choose a different team name.' };
            }
            return { error: `Failed to create team: ${rpcError?.message || 'Unknown error'}` };
        }

        // Update team with verification data
        if (verificationData.verified) {
            const { error: updateError } = await supabase
                .from('teams')
                .update({
                    verified: verificationData.verified,
                    verification_method: verificationData.verification_method,
                    verified_by: authUser.id,
                    verified_at: new Date().toISOString(),
                    github_repo_id: verificationData.github_repo_id
                })
                .eq('id', teamData.id);

            if (updateError) {
                console.warn('[Team Creation] Failed to update verification data:', updateError);
                // Don't fail the team creation, just log the warning
            }
        }

        return { team: teamData, joinCode: teamData.join_code };
    } catch (error) {
        return { error: `Team creation failed: ${error}` };
    }
}
 
 
//Joins a team using a join code
 
export async function joinTeam(joinCode: string): Promise<{ team?: Team; error?: string; alreadyMember?: boolean }> {
    try {
        // Check authentication
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { error: 'User must be authenticated to join a team' };
        }
 
        // Check current project Git requirements
        const currentProject = getCurrentProjectInfo();
        if (!currentProject) {
            return { error: 'No workspace folder is open. Please open the project folder to join a team.' };
        }
 
        if (!currentProject.isGitRepo || !currentProject.remoteUrl) {
            return { 
                error: 'Team functionality requires a Git repository with a remote origin.\n\n' +
                       'To join a team, you must have the team\'s Git repository cloned locally:\n' +
                       '1. Clone the team\'s repository: git clone <repository-url>\n' +
                       '2. Open the cloned project folder in VS Code\n' +
                       '3. Then try joining the team again\n\n' +
                       'Make sure you have the correct repository that matches the team\'s project.'
            };
        }
 
        const supabase = await getSupabaseClient();
 
        // Get the current Supabase auth user
        const { data: { user: authUser }, error: authUserError } = await supabase.auth.getUser();
        if (authUserError || !authUser) {
            return { error: `Failed to get current user: ${authUserError?.message || 'No user in session'}` };
        }
 
        // Find team by join code via secure RPC (bypasses RLS safely)
        const { data: teamData, error: teamError } = await supabase
            .rpc('get_team_by_join_code', { p_join_code: joinCode.toUpperCase() })
            .maybeSingle();
 
        if (teamError || !teamData) {
            return { error: `Invalid join code or team not found${teamError ? `: ${teamError.message}` : ''}` };
        }
 
        // If the user is already a member of this team, returns a already apart of team status
        const { data: existingMembership } = await supabase
            .from('team_membership')
            .select('id')
            .eq('team_id', teamData.id)
            .eq('user_id', authUser.id)
            .maybeSingle();
 
        if (existingMembership) {
            return { team: teamData, alreadyMember: true };
        }
 
        // CRITICAL: Validate that user's current project matches team's project
        const { validateCurrentProject } = require('./project-detection-service');
        const validation = validateCurrentProject(teamData.project_identifier, teamData.project_repo_url);
        
        if (!validation.isMatch) {
            const teamRepoUrl = teamData.project_repo_url || 'the team repository';
            const currentRepoUrl = validation.currentProject?.remoteUrl || 'your current project';
            
            return { 
                error: `Project mismatch! You cannot join this team.\n\n` +
                       `Team Repository: ${teamRepoUrl}\n` +
                       `Your Repository: ${currentRepoUrl}\n\n` +
                       `To join team "${teamData.lobby_name}":\n` +
                       `1. Clone the team repository: git clone ${teamRepoUrl}\n` +
                       `2. Open the cloned folder in VS Code\n` +
                       `3. Try joining again with the same join code\n\n` +
                       `${validation.reason || 'Repository URLs do not match'}`
            };
        }
 
        // Add user as member using secure RPC
        const { error: joinErr } = await supabase
            .rpc('join_team_by_code', { p_join_code: joinCode.toUpperCase() });
 
        if (joinErr) {
            return { error: `Failed to join team: ${joinErr.message}` };
        }
 
        // Emit participant status event (joined)
        try {
            await insertParticipantStatusEvent(teamData.id, authUser.id, [authUser.id], []);
        } catch (e) {
            console.warn('[joinTeam] Failed to insert participant status event:', e);
        }
 
        return { team: teamData };
    } catch (error) {
        return { error: `Team join failed: ${error}` };
    }
}
 
 
//Gets all teams the current user is a member of
 
export async function getUserTeams(): Promise<{ teams?: TeamWithMembership[]; error?: string }> {
    try {
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { error: 'User must be authenticated to get teams' };
        }
 
        const supabase = await getSupabaseClient();
 
        // Get the current Supabase auth user
        const { data: currentUser, error: getUserErr } = await supabase.auth.getUser();
        if (getUserErr || !currentUser.user) {
            return { error: 'User not authenticated' };
        }
 
        const { data, error } = await supabase
            .from('team_membership')
            .select(`
                role,
                teams (
                    id,
                    lobby_name,
                    created_by,
                    join_code,
                    created_at,
                    project_identifier,
                    project_repo_url,
                    project_name
                )
            `)
            .eq('user_id', currentUser.user.id);
 
        if (error) {
            return { error: `Failed to get teams: ${error.message}` };
        }
 
        const teams: TeamWithMembership[] = data.map((membership: any) => ({
            ...membership.teams,
            role: membership.role
        }));
 
        return { teams };
    } catch (error) {
        return { error: `Get teams failed: ${error}` };
    }
}
 
//Gets team details by ID (only if user is a member)
export async function getTeamById(teamId: string): Promise<{ team?: TeamWithMembership; error?: string }> {
    try {
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { error: 'User must be authenticated' };
        }
 
        const supabase = await getSupabaseClient();
 
        const { data: currentUser, error: getUserErr } = await supabase.auth.getUser();
        if (getUserErr || !currentUser.user) {
            return { error: 'User not authenticated' };
        }
 
        const { data, error } = await supabase
            .from('team_membership')
            .select(`
                role,
                teams (
                    id,
                    lobby_name,
                    created_by,
                    join_code,
                    created_at
                )
            `)
            .eq('team_id', teamId)
            .eq('user_id', currentUser.user.id)
            .single();
 
        if (error || !data) {
            return { error: 'Team not found or access denied' };
        }
 
        const team: TeamWithMembership = {
            ...data.teams,
            role: data.role
        };
 
        return { team };
    } catch (error) {
        return { error: `Get team failed: ${error}` };
    }
}
 
 
//Validates if the current workspace matches the team's project
export async function validateTeamProject(teamId: string): Promise<{
    isValid: boolean;
    team?: Team;
    currentProject?: ProjectInfo;
    error?: string;
    validationMessage?: string;
}> {
    try {
        // Get team information
        const { team, error: teamError } = await getTeamById(teamId);
        if (teamError || !team) {
            return { isValid: false, error: teamError || 'Team not found' };
        }

        // Get current project info
        const currentProject = getCurrentProjectInfo();
        if (!currentProject) {
            return {
                isValid: false,
                team,
                error: 'No workspace folder is currently open'
            };
        }

        // Validate project match
        const validation = validateCurrentProject(team.project_identifier, team.project_repo_url);
        
        return {
            isValid: validation.isMatch,
            team,
            currentProject: validation.currentProject || undefined,
            validationMessage: validation.reason
        };
    } catch (error) {
        return { isValid: false, error: `Validation failed: ${error}` };
    }
}
 
//Shows a warning dialog when project mismatch is detected
export async function handleProjectMismatch(team: Team, currentProject: ProjectInfo): Promise<'continue' | 'switch' | 'cancel'> {
    const teamProjectDesc = team.project_repo_url 
        ? `${team.project_name || 'Team Project'} (${team.project_repo_url})`
        : team.project_name || 'Team Project';
    
    const currentProjectDesc = getProjectDescription(currentProject);
    
    const action = await vscode.window.showWarningMessage(
        `Project Mismatch Detected!\n\n` +
        `Your team is linked to: ${teamProjectDesc}\n` +
        `But you have open: ${currentProjectDesc}\n\n` +
        `This could lead to tracking issues or unintended changes being shared with your team.`,
        { modal: true },
        'Continue Anyway',
        'Switch to Team Project',
        'Cancel'
    );
 
    switch (action) {
        case 'Continue Anyway':
            return 'continue';
        case 'Switch to Team Project':
            return 'switch';
        default:
            return 'cancel';
    }
}
 
 
//Attempts to open the team's project in VS Code
export async function openTeamProject(team: Team): Promise<{ success: boolean; error?: string }> {
    try {
        if (team.project_repo_url) {
            // If it's a Git repository, suggest cloning it
            const cloneAction = await vscode.window.showInformationMessage(
                `To work on the team project, you need to clone the repository:\n${team.project_repo_url}`,
                'Clone Repository',
                'Cancel'
            );
 
            if (cloneAction === 'Clone Repository') {
                await vscode.commands.executeCommand('git.clone', team.project_repo_url);
                return { success: true };
            }
        } else {
            // If it's not a Git repo, we can't automatically switch
            await vscode.window.showInformationMessage(
                `This team is linked to a local project: ${team.project_name}\n\n` +
                `Please manually open the correct project folder in VS Code.`,
                'OK'
            );
        }
        
        return { success: false };
    } catch (error) {
        return { success: false, error: `Failed to open project: ${error}` };
    }
}
 
//Gets a user-friendly description of the team's project
export function getTeamProjectDescription(team: Team): string {
    if (team.project_repo_url) {
        return `${team.project_name || 'Team Project'} (${team.project_repo_url})`;
    }
    return team.project_name || 'Local Project';
}
 
 
//Updates a team's project information (admin only)
export async function updateTeamProject(teamId: string): Promise<{ success: boolean; error?: string }> {
    try {
        // Check if user is admin of the team
        const { team, error: teamError } = await getTeamById(teamId);
        if (teamError || !team) {
            return { success: false, error: teamError || 'Team not found' };
        }

        if (team.role !== 'admin') {
            return { success: false, error: 'Only team admins can update project information' };
        }

        // Get current project information
        const currentProject = getCurrentProjectInfo();
        if (!currentProject) {
            return { success: false, error: 'No workspace folder is open. Please open the project you want to link to this team.' };
        }

        const supabase = await getSupabaseClient();

        // Update team with new project information
        const { error: updateError } = await supabase
            .from('teams')
            .update({
                project_repo_url: currentProject.remoteUrl,
                project_identifier: currentProject.projectHash,
                project_name: currentProject.projectName
            })
            .eq('id', teamId);

        if (updateError) {
            return { success: false, error: `Failed to update team project: ${updateError.message}` };
        }

        await vscode.window.showInformationMessage(
            `Team project updated!\n\nTeam "${team.lobby_name}" is now linked to: ${getProjectDescription(currentProject)}`
        );

        return { success: true };
    } catch (error) {
        return { success: false, error: `Update failed: ${error}` };
    }
}
 
// Deletes a team (admin only). Removes memberships first, then deletes the team.
export async function deleteTeam(teamId: string): Promise<{ success: boolean; error?: string }> {
    try {
        // Ensure authenticated
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { success: false, error: 'User must be authenticated to delete a team' };
        }

        const supabase = await getSupabaseClient();

        // Get current user id from session
        const { data: currentUser, error: getUserErr } = await supabase.auth.getUser();
        if (getUserErr || !currentUser.user) {
            return { success: false, error: 'User not authenticated' };
        }

        // Check admin role for this team
        const { data: membership, error: membershipErr } = await supabase
            .from('team_membership')
            .select('role')
            .eq('team_id', teamId)
            .eq('user_id', currentUser.user.id)
            .single();

        if (membershipErr || !membership) {
            return { success: false, error: 'You are not a member of this team or access is denied' };
        }
        if (membership.role !== 'admin') {
            return { success: false, error: 'Only the Team Admin can delete this team' };
        }

        // Perform deletion via secure RPC to avoid RLS no-op deletes
        const { data: deletedOk, error: deleteTeamErr } = await supabase
            .rpc('delete_team', { p_team_id: teamId });
        if (deleteTeamErr || deletedOk !== true) {
            return { success: false, error: deleteTeamErr?.message || 'Failed to delete team' };
        }

        return { success: true };
    } catch (error) {
        return { success: false, error: `Delete failed: ${error}` };
    }
}
 
// Leaves a team (member only): removes the current user's membership row for the team
export async function leaveTeam(teamId: string): Promise<{ success: boolean; error?: string }> {
    try {
        // Ensure authenticated
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { success: false, error: 'User must be authenticated to leave a team' };
        }
 
        const supabase = await getSupabaseClient();
 
        // Get current user's id
        const { data: currentUser, error: getUserErr } = await supabase.auth.getUser();
        if (getUserErr || !currentUser.user) {
            return { success: false, error: 'User not authenticated' };
        }
 
        // Ensure the user is a member of this team
        const { data: membership, error: membershipErr } = await supabase
            .from('team_membership')
            .select('role')
            .eq('team_id', teamId)
            .eq('user_id', currentUser.user.id)
            .maybeSingle();
 
        if (membershipErr) {
            return { success: false, error: `Failed to check membership: ${membershipErr.message}` };
        }
        if (!membership) {
            return { success: false, error: 'You are not a member of this team' };
        }
        if (membership.role === 'admin') {
            return { success: false, error: 'Team admins cannot leave the team they administer. Transfer admin role or delete the team.' };
        }
 
        // Use secure RPC to ensure we know if a row was actually deleted
        const { data: leftOk, error: deleteErr } = await supabase
            .rpc('leave_team', { p_team_id: teamId });
 
        if (deleteErr || leftOk !== true) {
            return { success: false, error: deleteErr?.message || 'Unable to leave team (no membership removed)' };
        }
 
        // Emit participant status event (left)
        try {
            await insertParticipantStatusEvent(teamId, currentUser.user.id, [], [currentUser.user.id]);
        } catch (e) {
            console.warn('[leaveTeam] Failed to insert participant status event:', e);
        }
 
        return { success: true };
    } catch (error) {
        return { success: false, error: `Leave team failed: ${error}` };
    }
}
 
// Gets all members of a team (only accessible to team members)
export interface TeamMember {
    id: string;
    userId: string;
    role: 'member' | 'admin';
    joinedAt: string;
    email: string;
    firstName?: string;
    lastName?: string;
    displayName?: string;
    skills?: string[];
}
 
export async function getTeamMembers(teamId: string): Promise<{ members?: TeamMember[]; error?: string }> {
    try {
        // Check authentication
        const { context: user, error: authError } = await getAuthContext();
        if (authError || !user?.isAuthenticated) {
            return { error: 'User must be authenticated to view team members' };
        }
 
        const supabase = await getSupabaseClient();
 
        // Get current user's auth ID
        const { data: currentUser, error: getUserErr } = await supabase.auth.getUser();
        if (getUserErr || !currentUser.user) {
            return { error: 'User not authenticated' };
        }
 
        // Verify that the current user is a member of this team (security check)
        const { data: userMembership, error: membershipErr } = await supabase
            .from('team_membership')
            .select('id')
            .eq('team_id', teamId)
            .eq('user_id', currentUser.user.id)
            .maybeSingle();
 
        if (membershipErr || !userMembership) {
            return { error: 'You must be a member of this team to view its members' };
        }
 
        // Create an RPC function call to fetch team members with user data
        // This uses a stored procedure that can access auth.users with proper permissions
        const { data: rpcData, error: rpcError } = await supabase
            .rpc('get_team_members_with_users', { p_team_id: teamId });
 
        if (rpcError) {
            // If RPC doesn't exist, fall back to basic query
            console.warn('[getTeamMembers] RPC function not available, using fallback:', rpcError);

            const { data: fallbackData, error: fallbackError } = await supabase
                .from('team_membership')
                .select('id, user_id, role, joined_at')
                .eq('team_id', teamId)
                .order('role', { ascending: false })
                .order('joined_at', { ascending: true });

            if (fallbackError) {
                console.error('[getTeamMembers] Fallback query failed:', fallbackError);
                return { error: `Failed to fetch team members: ${fallbackError.message}` };
            }

            // Fetch user profiles separately
            const userIds = fallbackData.map((m: any) => m.user_id);
            const { data: profilesData, error: profilesError } = await supabase
                .from('user_profiles')
                .select('user_id, name, interests, custom_skills')
                .in('user_id', userIds);

            console.log('[getTeamMembers] Profiles data fetched:', profilesData);
            if (profilesError) {
                console.error('[getTeamMembers] Error fetching profiles:', profilesError);
            }

            // Create a map of profiles
            const profilesMap = new Map(profilesData?.map((p: any) => [p.user_id, p]) || []);

            // Return members with user_profiles data
            const members: TeamMember[] = fallbackData.map((membership: any) => {
                const profile = profilesMap.get(membership.user_id) as any;
                console.log('[getTeamMembers] Profile for user', membership.user_id, ':', profile);

                // Combine interests and custom_skills
                let skills: string[] = [];
                if (profile?.interests && Array.isArray(profile.interests) && profile.interests.length > 0) {
                    skills = [...profile.interests];
                }
                if (profile?.custom_skills && Array.isArray(profile.custom_skills) && profile.custom_skills.length > 0) {
                    skills = [...skills, ...profile.custom_skills];
                }

                console.log('[getTeamMembers] Skills for user', membership.user_id, ':', skills);

                return {
                    id: membership.id,
                    userId: membership.user_id,
                    role: membership.role,
                    joinedAt: membership.joined_at,
                    email: membership.user_id, // Fallback - no auth.users access
                    displayName: profile?.name || undefined,
                    skills: skills,
                    firstName: undefined,
                    lastName: undefined
                };
            });

            return { members };
        }
 
        // Transform RPC response into TeamMember format
        const members: TeamMember[] = rpcData.map((row: any) => {
            let firstName: string | undefined;
            let lastName: string | undefined;
 
            // Parse full_name from user metadata if available
            if (row.full_name) {
                const nameParts = row.full_name.split(' ');
                firstName = nameParts[0];
                lastName = nameParts.slice(1).join(' ') || undefined;
            }
 
            // Get skills from user profile if available (from enhanced RPC)
            let skills: string[] = [];
            if (row.interests && Array.isArray(row.interests)) {
                skills = [...row.interests];
            }
            if (row.custom_skills && Array.isArray(row.custom_skills)) {
                skills = [...skills, ...row.custom_skills];
            }
 
            return {
                id: row.membership_id,
                userId: row.user_id,
                role: row.role,
                joinedAt: row.joined_at,
                email: row.email || 'Unknown',
                displayName: row.display_name || row.full_name || undefined,
                firstName,
                lastName,
                skills: Array.isArray(skills) ? skills : []
            };
        });
 
        // Fetch user profiles for skills if not already included in RPC response
        if (members.length > 0 && !rpcData[0]?.interests && !rpcData[0]?.custom_skills) {
            console.log('[getTeamMembers] RPC did not include skills, fetching from user_profiles...');
            const userIds = members.map(m => m.userId);
 
            const { data: profilesData, error: profilesError } = await supabase
                .from('user_profiles')
                .select('user_id, name, interests, custom_skills')
                .in('user_id', userIds);
 
            if (!profilesError && profilesData) {
                // Map profiles to members
                const profilesMap = new Map(profilesData.map((p: any) => [p.user_id, p]));
 
                members.forEach(member => {
                    const profile = profilesMap.get(member.userId) as any;
                    if (profile) {
                        let skills: string[] = [];
                        if (profile.interests && Array.isArray(profile.interests)) {
                            skills = [...profile.interests];
                        }
                        if (profile.custom_skills && Array.isArray(profile.custom_skills)) {
                            skills = [...skills, ...profile.custom_skills];
                        }
                        member.skills = skills;
                        if (!member.displayName && profile.name) {
                            member.displayName = profile.name;
                        }
                    }
                });
            }
        }
 
        return { members };
    } catch (error) {
        return { error: `Failed to get team members: ${error}` };
    }
}