All files / src/views TasksPanel.ts

48.48% Statements 385/794
56.25% Branches 27/48
58.33% Functions 14/24
48.48% Lines 385/794

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 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x   15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x         2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 15x 15x 15x 15x 15x 15x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 5x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 15x 15x 15x 15x 15x 15x       15x 15x 15x 15x 15x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 15x 15x 15x 15x 15x                                                                                   15x 15x 15x 15x 15x                                                                       15x 15x 15x 15x 15x 1x                 1x 15x 15x 15x 15x 15x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x       3x 15x 15x 15x 15x 15x                                                                   15x 15x 15x 15x 15x                                                                   15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x           2x 15x 15x 15x 15x 15x                                               15x 15x 15x 15x 15x                                               15x 15x 15x 15x 15x                                         15x 15x 15x 15x 15x                                                     15x 15x 15x 15x 15x 2x         2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x                                                                                                                                                                                                                                                                   15x  
import * as vscode from 'vscode';
import { JiraService } from '../services/jira-service';
import * as fs from 'fs';
import * as path from 'path';
 
/**
 * Tasks panel for displaying Jira issues/tasks for teams.
 * Admin users can configure Jira integration, all team members can view tasks.
 */
export class TasksPanel {
    private _context: vscode.ExtensionContext;
    private _view?: vscode.WebviewView;
    private _currentTeamId?: string | null;
    private _currentUserRole?: string | null;
    private _jiraConfigured: boolean = false;
 
    // Rate limiting for AI Suggestions
    private _lastAISuggestionTime: Map<string, number> = new Map(); // teamId -> timestamp
    private readonly AI_COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes
    private readonly MAX_TASKS_TO_ANALYZE = 25; // Maximum tasks to send to AI
 
    constructor(context: vscode.ExtensionContext) {
        this._context = context;
    }
 
    /**
     * Sets the webview for message handling
     */
    public setWebview(view: vscode.WebviewView) {
        this._view = view;
        this.setupMessageHandling();
    }
 
    /**
     * Gets the HTML content for the Tasks panel.
     * Shows different UI based on user role and Jira connection status.
     */
    public getHtml(): string {
        // Read HTML from external file
        const htmlPath = path.join(this._context.extensionPath, 'media', 'tasksPanel.html');
        let htmlContent = '';
        try {
            htmlContent = fs.readFileSync(htmlPath, 'utf8');
        } catch (e) {
            console.error('Failed to read tasksPanel.html:', e);
            return '<div>Failed to load Tasks panel.</div>';
        }
 
        // Read JavaScript from external file
        const jsPath = path.join(this._context.extensionPath, 'media', 'tasksPanel.js');
        let jsContent = '';
        try {
            jsContent = fs.readFileSync(jsPath, 'utf8');
        } catch (e) {
            console.error('Failed to read tasksPanel.js:', e);
            jsContent = '';
        }
 
        return `${htmlContent}
            <script>
            ${jsContent}
            </script>
        `;
    }
 
    /**
     * Sets up message handling for the webview
     */
    private setupMessageHandling() {
        if (!this._view) return;
 
        // Note: Message handling is done in MainPanel.handleTasksMessage
        // This is kept for reference but messages won't reach here
        this._view.webview.onDidReceiveMessage(async (message: any) => {
            console.log('TasksPanel received message (note: MainPanel handles these):', message);
        });
    }
 
    /**
     * Initializes the panel with current team and user state
     */
    public async initializePanel() {
        await this.refreshTeamState();
 
        // Give the webview a moment to fully load before sending the first message
        setTimeout(() => {
            this.updateUI();
        }, 100);
    }
 
    /**
     * Refreshes the current team and user state
     */
    private async refreshTeamState() {
        try {
            // Get current team from global state
            this._currentTeamId = this._context.globalState.get<string>('collabAgent.currentTeam');
 
            if (!this._currentTeamId) {
                this._currentUserRole = null;
                this._jiraConfigured = false;
                return;
            }
 
            // Get user role for this team
            const { getUserTeams } = require('../services/team-service');
            const result = await getUserTeams();
            if (result.error) {
                console.error('Failed to get user teams:', result.error);
                this._currentUserRole = null;
                this._jiraConfigured = false;
                return;
            }
 
            const currentTeam = result.teams?.find((t: any) => t.id === this._currentTeamId);
            this._currentUserRole = currentTeam?.role || null;
 
            // Check if Jira is configured for this team
            const jiraService = JiraService.getInstance();
            const jiraConfig = await jiraService.getJiraConfig(this._currentTeamId);
            this._jiraConfigured = !!jiraConfig;
 
        } catch (error) {
            console.error('Failed to refresh team state:', error);
            this._currentTeamId = null;
            this._currentUserRole = null;
            this._jiraConfigured = false;
        }
    }
 
    /**
     * Updates the UI based on current state
     * Implements the three workflows: Admin setup, Non-admin waiting, All users (post-setup)
     */
    private updateUI() {
        if (!this._view) return;
 
        // Workflow 1 & 2: No team or not a team member
        if (!this._currentTeamId || !this._currentUserRole) {
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showSetup: false,
                showWaiting: false,
                showTasks: false,
                showError: true,
                showLoading: false,
                statusText: 'No team selected',
                errorMessage: !this._currentTeamId
                    ? 'Please select a team from the Teams tab.'
                    : 'Please select a team from the Teams tab.'
            });
            return;
        }
 
        // Workflow 2: Jira not configured
        if (!this._jiraConfigured) {
            if (this._currentUserRole === 'admin') {
                // Admin Workflow: Show setup instructions
                this._view.webview.postMessage({
                    command: 'updateTasksUI',
                    showSetup: true,
                    showWaiting: false,
                    showTasks: false,
                    showError: false,
                    showLoading: false,
                    statusText: 'Setup required'
                });
            } else {
                // Non-Admin Workflow: Show waiting message
                this._view.webview.postMessage({
                    command: 'updateTasksUI',
                    showSetup: false,
                    showWaiting: true,
                    showTasks: false,
                    showError: false,
                    showLoading: false,
                    statusText: 'Waiting for admin setup'
                });
            }
            return;
        }
 
        // Workflow 3: Jira is configured - show tasks for all users
        this._view.webview.postMessage({
            command: 'updateTasksUI',
            showSetup: false,
            showWaiting: false,
            showTasks: true,
            showError: false,
            showLoading: true,
            statusText: 'Loading tasks...'
        });
 
        // Load tasks and sprints from Jira
        this.loadTasks();
        this.loadSprints();
    }
 
    /**
     * Updates the panel state - refreshes team state and UI
     * Called externally when team changes
     */
    public async updatePanelState() {
        await this.refreshTeamState();
        this.updateUI();
    }
 
    /**
     * Loads and displays Jira tasks for the current team.
     */
    public async loadTasks() {
        if (!this._currentTeamId || !this._view) return;
 
        try {
            const jiraService = JiraService.getInstance();
            const issues = await jiraService.fetchTeamIssues(this._currentTeamId);
 
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: true,
                showLoading: false,
                showError: false,
                statusText: `${issues.length} tasks loaded`,
                tasks: issues,
                isAdmin: this._currentUserRole === 'admin'
            });
 
        } catch (error) {
            console.error('Failed to load tasks:', error);
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: false,
                showError: true,
                showLoading: false,
                statusText: 'Error loading tasks',
                errorMessage: error instanceof Error ? error.message : 'Failed to load tasks'
            });
        }
    }
 
    /**
     * Handles Jira connection for team admin.
     */
    public async handleConnectJira() {
        if (!this._currentTeamId || this._currentUserRole !== 'admin') return;
 
        try {
            const jiraService = JiraService.getInstance();
 
            // Get current user ID
            const { getAuthContext } = require('../services/auth-service');
            const authResult = await getAuthContext();
            const adminUserId = authResult?.context?.id;
 
            if (!adminUserId) {
                throw new Error('Unable to get user authentication context');
            }
 
            await jiraService.initiateJiraAuth(this._currentTeamId, adminUserId);
 
            // Refresh state after successful connection
            await this.refreshTeamState();
            this.updateUI();
 
        } catch (error) {
            console.error('Failed to connect Jira:', error);
            vscode.window.showErrorMessage(
                `Failed to connect to Jira: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
        }
    }
 
    /**
     * Handles Jira connection with credentials provided directly from the form.
     */
    public async handleConnectJiraWithCredentials(jiraUrl: string, jiraEmail: string, jiraToken: string) {
        // Security check: Only admins can connect Jira
        if (!this._currentTeamId || this._currentUserRole !== 'admin') {
            this._view?.webview.postMessage({
                command: 'jiraConnectionFailed',
                error: 'Only team admins can configure Jira integration'
            });
            return;
        }

        try {
            const jiraService = JiraService.getInstance();

            // Get current user ID
            const { getAuthContext } = require('../services/auth-service');
            const authResult = await getAuthContext();
            const adminUserId = authResult?.context?.id;

            if (!adminUserId) {
                throw new Error('Unable to get user authentication context');
            }

            // Call the service method that handles the full flow with provided credentials
            await jiraService.connectWithCredentials(this._currentTeamId, adminUserId, jiraUrl, jiraEmail, jiraToken);

            // Send success message to webview
            this._view?.webview.postMessage({
                command: 'jiraConnected'
            });

            // Refresh state after successful connection
            await this.refreshTeamState();
            this.updateUI();

        } catch (error) {
            console.error('Failed to connect Jira with credentials:', error);
            this._view?.webview.postMessage({
                command: 'jiraConnectionFailed',
                error: error instanceof Error ? error.message : 'Failed to connect to Jira'
            });
        }
    }
 
    /**
     * Handles disconnecting from Jira (admin only).
     */
    public async handleDisconnectJira() {
        // Security check: Only admins can disconnect Jira
        if (!this._currentTeamId || this._currentUserRole !== 'admin') {
            vscode.window.showErrorMessage('Only team admins can disconnect Jira integration');
            return;
        }

        // Show confirmation dialog
        const confirmation = await vscode.window.showWarningMessage(
            'Are you sure you want to disconnect from Jira? This will remove the Jira integration for your team.',
            { modal: true },
            'Disconnect',
            'Cancel'
        );

        if (confirmation !== 'Disconnect') {
            return;
        }

        try {
            const jiraService = JiraService.getInstance();
            await jiraService.disconnectJira(this._currentTeamId);

            vscode.window.showInformationMessage('Successfully disconnected from Jira');

            // Refresh state to show setup UI again
            await this.refreshTeamState();
            this.updateUI();

        } catch (error) {
            console.error('Failed to disconnect Jira:', error);
            vscode.window.showErrorMessage(
                error instanceof Error ? error.message : 'Failed to disconnect from Jira'
            );
        }
    }
 
    /**
     * Handles refreshing tasks.
     */
    public async handleRefreshTasks() {
        if (this._jiraConfigured) {
            this._view?.webview.postMessage({
                command: 'updateTasksUI',
                showLoading: true,
                statusText: 'Refreshing tasks...'
            });
            await this.loadTasks();
            await this.loadSprints();
        }
    }
 
    /**
     * Loads sprints and updates the sprint filter dropdown.
     */
    public async loadSprints() {
        if (!this._currentTeamId || !this._view || !this._jiraConfigured) return;
 
        try {
            const jiraService = JiraService.getInstance();
            const sprints = await jiraService.fetchSprints(this._currentTeamId);
 
            this._view.webview.postMessage({
                command: 'updateSprints',
                sprints: sprints
            });
 
        } catch (error) {
            console.error('Failed to load sprints:', error);
            // Don't show error to user, just log it
        }
    }
 
    /**
     * Loads issues for a specific sprint.
     */
    public async handleLoadSprint(sprintId: number) {
        if (!this._currentTeamId || !this._view) return;

        try {
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showLoading: true,
                statusText: 'Loading sprint issues...'
            });

            const jiraService = JiraService.getInstance();
            const issues = await jiraService.fetchSprintIssues(this._currentTeamId, sprintId);

            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: true,
                showLoading: false,
                showError: false,
                statusText: `${issues.length} issues in sprint`,
                tasks: issues
            });

        } catch (error) {
            console.error('Failed to load sprint issues:', error);
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: false,
                showError: true,
                showLoading: false,
                statusText: 'Error loading sprint',
                errorMessage: error instanceof Error ? error.message : 'Failed to load sprint issues'
            });
        }
    }
 
    /**
     * Loads backlog issues.
     */
    public async handleLoadBacklog() {
        if (!this._currentTeamId || !this._view) return;

        try {
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showLoading: true,
                statusText: 'Loading backlog...'
            });

            const jiraService = JiraService.getInstance();
            const issues = await jiraService.fetchBacklogIssues(this._currentTeamId);

            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: true,
                showLoading: false,
                showError: false,
                statusText: `${issues.length} issues in backlog`,
                tasks: issues
            });

        } catch (error) {
            console.error('Failed to load backlog:', error);
            this._view.webview.postMessage({
                command: 'updateTasksUI',
                showTasks: false,
                showError: true,
                showLoading: false,
                statusText: 'Error loading backlog',
                errorMessage: error instanceof Error ? error.message : 'Failed to load backlog issues'
            });
        }
    }
 
    /**
     * Handles transitioning an issue to a new status.
     */
    public async handleTransitionIssue(issueKey: string, targetStatus: string) {
        if (!this._currentTeamId) return;
 
        try {
            const jiraService = JiraService.getInstance();
 
            // First, get available transitions for the issue
            const transitions = await jiraService.fetchTransitions(this._currentTeamId, issueKey);
 
            // Find the transition that matches the target status
            const transition = transitions.find(t => t.to.name === targetStatus);
 
            if (!transition) {
                vscode.window.showErrorMessage(`Cannot transition ${issueKey} to "${targetStatus}". This transition may not be available.`);
                return;
            }
 
            // Perform the transition
            await jiraService.transitionIssue(this._currentTeamId, issueKey, transition.id);
 
            vscode.window.showInformationMessage(`✅ ${issueKey} moved to "${targetStatus}"`);
 
            // Refresh the task list to show updated status
            await this.handleRefreshTasks();
 
        } catch (error) {
            console.error('Failed to transition issue:', error);
            vscode.window.showErrorMessage(
                `Failed to transition ${issueKey}: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
        }
    }
 
    /**
     * Handles reassigning a task to a different user.
     */
    public async handleReassignIssue(issueKey: string, accountId: string | null) {
        if (!this._currentTeamId) return;

        try {
            const jiraService = JiraService.getInstance();

            // Performs the reassignment
            await jiraService.reassignIssue(this._currentTeamId, issueKey, accountId);

            const message = accountId 
                ? `✅ ${issueKey} has been reassigned`
                : `✅ ${issueKey} has been unassigned`;
            vscode.window.showInformationMessage(message);

            // Refresh the task list to show updated assignee
            await this.handleRefreshTasks();

        } catch (error) {
            console.error('Failed to reassign issue:', error);
            vscode.window.showErrorMessage(
                `Failed to reassign ${issueKey}: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
        }
    }
 
    /**
     * Handles updating story points for an issue.
     */
    public async handleUpdateStoryPoints(issueKey: string, storyPoints: number | null) {
        if (!this._currentTeamId) return;

        try {
            const jiraService = JiraService.getInstance();

            // Performs the update
            await jiraService.updateStoryPoints(this._currentTeamId, issueKey, storyPoints);

            const message = storyPoints !== null
                ? `✅ ${issueKey} story points updated to ${storyPoints}`
                : `✅ ${issueKey} story points cleared`;
            vscode.window.showInformationMessage(message);

            // Refresh the task list to show updated story points
            await this.handleRefreshTasks();

        } catch (error) {
            console.error('Failed to update story points:', error);
            vscode.window.showErrorMessage(
                `Failed to update story points for ${issueKey}: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
        }
    }
 
    /**
     * Handles updating priority for an issue.
     */
    public async handleUpdatePriority(issueKey: string, priorityName: string) {
        if (!this._currentTeamId) return;

        try {
            const jiraService = JiraService.getInstance();

            // Performs the update
            await jiraService.updatePriority(this._currentTeamId, issueKey, priorityName);

            vscode.window.showInformationMessage(`✅ ${issueKey} priority updated to ${priorityName}`);

            // Refresh the task list to show updated priority
            await this.handleRefreshTasks();

        } catch (error) {
            console.error('Failed to update priority:', error);
            vscode.window.showErrorMessage(
                `Failed to update priority for ${issueKey}: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
        }
    }
 
    /**
     * Handles fetching assignable users for the project.
     */
    public async handleFetchAssignableUsers() {
        if (!this._currentTeamId || !this._view) {
            console.warn('[handleFetchAssignableUsers] No team ID or view available');
            return;
        }

        try {
            console.log('[handleFetchAssignableUsers] Fetching assignable users for team:', this._currentTeamId);
            const jiraService = JiraService.getInstance();
            const users = await jiraService.fetchAssignableUsers(this._currentTeamId);
            console.log('[handleFetchAssignableUsers] Fetched users:', users.length, 'users');

            // Send users to webview
            this._view.webview.postMessage({
                command: 'assignableUsers',
                users: users
            });

        } catch (error) {
            console.error('[handleFetchAssignableUsers] Failed to fetch assignable users:', error);
            this._view?.webview.postMessage({
                command: 'assignableUsers',
                users: [],
                error: error instanceof Error ? error.message : 'Unknown error'
            });
        }
    }
 
    /**
     * Handles creating a new Jira task.
     */
    public async handleCreateTask(taskData: any) {
        if (!this._currentTeamId || !this._view) {
            vscode.window.showErrorMessage('No team selected or view not available.');
            this._view?.webview.postMessage({ command: 'taskCreationFailed' });
            return;
        }
 
        try {
            const jiraService = JiraService.getInstance();
 
            // Create the issue in Jira
            const newIssue = await jiraService.createIssue(this._currentTeamId, taskData);
 
            // Show success message with the issue key
            vscode.window.showInformationMessage(`✅ Task created successfully: ${newIssue.key}`);
 
            // Notify webview of success
            this._view.webview.postMessage({ command: 'taskCreated' });
 
            // Refresh the task list to show the new task
            await this.handleRefreshTasks();
 
        } catch (error) {
            console.error('Failed to create task:', error);
            vscode.window.showErrorMessage(
                `Failed to create task: ${error instanceof Error ? error.message : 'Unknown error'}`
            );
 
            // Notify webview of failure
            this._view?.webview.postMessage({ command: 'taskCreationFailed' });
        }
    }
 
    /**
     * Handles AI task recommendations for unassigned tasks.
     * Fetches unassigned tasks, calls AI API to get recommendations,
     * and posts them to the team activity timeline.
     *
     * Rate limiting:
     * - 5-minute cooldown between requests per team
     * - Maximum 25 tasks analyzed per request
     */
    public async handleGetAISuggestions() {
        if (!this._currentTeamId || !this._view) {
            vscode.window.showErrorMessage('No team selected or view not available.');
            this._view?.webview.postMessage({ command: 'aiSuggestionsFailed' });
            return;
        }

        try {
            // Check rate limit: 5-minute cooldown per team
            const now = Date.now();
            const lastRequestTime = this._lastAISuggestionTime.get(this._currentTeamId);

            if (lastRequestTime) {
                const timeSinceLastRequest = now - lastRequestTime;
                const cooldownRemaining = this.AI_COOLDOWN_MS - timeSinceLastRequest;

                if (cooldownRemaining > 0) {
                    const minutesRemaining = Math.ceil(cooldownRemaining / 60000);
                    vscode.window.showWarningMessage(
                        `⏱️ Please wait ${minutesRemaining} minute${minutesRemaining > 1 ? 's' : ''} before requesting AI suggestions again. This helps us manage API costs.`
                    );
                    this._view.webview.postMessage({ command: 'aiSuggestionsFailed' });
                    return;
                }
            }

            // Show loading message
            vscode.window.showInformationMessage('Analyzing unassigned tasks...');

            const jiraService = JiraService.getInstance();

            // Fetch all team issues
            const allIssues = await jiraService.fetchTeamIssues(this._currentTeamId);

            // Filter for unassigned tasks
            const unassignedTasks = allIssues.filter(issue => !issue.fields.assignee);

            if (unassignedTasks.length === 0) {
                vscode.window.showInformationMessage('No unassigned tasks found.');
                this._view.webview.postMessage({ command: 'aiSuggestionsComplete' });
                return;
            }

            // Enforce task limit to manage API costs
            if (unassignedTasks.length > this.MAX_TASKS_TO_ANALYZE) {
                const result = await vscode.window.showWarningMessage(
                    `⚠️ Found ${unassignedTasks.length} unassigned tasks. To manage API costs, only the first ${this.MAX_TASKS_TO_ANALYZE} tasks will be analyzed. Continue?`,
                    'Yes, Continue',
                    'Cancel'
                );

                if (result !== 'Yes, Continue') {
                    vscode.window.showInformationMessage('AI analysis cancelled.');
                    this._view.webview.postMessage({ command: 'aiSuggestionsFailed' });
                    return;
                }
            }

            // Get current user ID for the API call
            const { getAuthContext } = require('../services/auth-service');
            const authResult = await getAuthContext();
            const userId = authResult?.context?.id;

            if (!userId) {
                throw new Error('Unable to get user authentication context');
            }

            // Prepare task data for AI analysis (limit to MAX_TASKS_TO_ANALYZE)
            const tasksToAnalyze = unassignedTasks.slice(0, this.MAX_TASKS_TO_ANALYZE);
            const tasksForAI = tasksToAnalyze.map(task => ({
                key: task.key,
                summary: task.fields.summary,
                description: task.fields.description || ''
            }));

            // Call the AI recommendations API
            // Note: Update TESTING flag in src/api/types/endpoints.ts after deploying to Render
            const { getApiUrl } = require('../api/types/endpoints');
            const response = await fetch(getApiUrl('/api/ai/task_recommendations'), {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    team_id: this._currentTeamId,
                    user_id: userId,
                    unassigned_tasks: tasksForAI
                })
            });

            if (!response.ok) {
                const errorData = await response.json();
                throw new Error(errorData.error || 'Failed to get AI recommendations');
            }

            const result = await response.json();

            // Record successful request timestamp for rate limiting
            this._lastAISuggestionTime.set(this._currentTeamId, now);

            // Show success message
            if (result.recommendations_count > 0) {
                const tasksAnalyzed = tasksForAI.length;
                const totalUnassigned = unassignedTasks.length;
                const taskCountMsg = tasksAnalyzed < totalUnassigned
                    ? ` (analyzed ${tasksAnalyzed} of ${totalUnassigned} tasks)`
                    : '';

                vscode.window.showInformationMessage(
                    `✅ AI posted ${result.recommendations_count} task recommendation(s) to the timeline!${taskCountMsg}`
                );
            } else {
                vscode.window.showInformationMessage(
                    'AI analysis complete, but no recommendations were generated.'
                );
            }

            // Notify webview that operation is complete
            this._view.webview.postMessage({ command: 'aiSuggestionsComplete' });

        } catch (error) {
            console.error('Failed to get AI suggestions:', error);
            vscode.window.showErrorMessage(
                `Failed to get AI suggestions: ${error instanceof Error ? error.message : 'Unknown error'}`
            );

            // Notify webview that operation failed
            this._view?.webview.postMessage({ command: 'aiSuggestionsFailed' });
        }
    }
}