import { logger } from "../monitoring/logger.js";
class TeamAnalytics {
  teamMembers = /* @__PURE__ */ new Map();
  handoffHistory = [];
  mergeHistory = [];
  analyticsCache = /* @__PURE__ */ new Map();
  /**
   * Add or update team member
   */
  addTeamMember(member) {
    this.teamMembers.set(member.userId, member);
    logger.info(`Team member added: ${member.userId}`, {
      role: member.role,
      skills: member.skills
    });
  }
  /**
   * Record handoff activity
   */
  recordHandoffActivity(progress, metadata) {
    this.handoffHistory.push(progress);
    this.invalidateCache("handoff");
    logger.debug(`Handoff activity recorded: ${progress.requestId}`, {
      status: progress.status,
      transferredFrames: progress.transferredFrames
    });
  }
  /**
   * Record merge activity
   */
  recordMergeActivity(session) {
    this.mergeHistory.push(session);
    this.invalidateCache("merge");
    logger.debug(`Merge activity recorded: ${session.sessionId}`, {
      conflicts: session.conflicts.length,
      resolutions: session.resolutions.length
    });
  }
  /**
   * Generate comprehensive collaboration metrics
   */
  async generateCollaborationMetrics(timeRange) {
    const cacheKey = `metrics-${timeRange.start.toISOString()}-${timeRange.end.toISOString()}`;
    if (this.analyticsCache.has(cacheKey)) {
      return this.analyticsCache.get(cacheKey);
    }
    const metrics = {
      timeRange,
      teamMetrics: await this.calculateTeamMetrics(timeRange),
      handoffMetrics: await this.calculateHandoffMetrics(timeRange),
      conflictMetrics: await this.calculateConflictMetrics(timeRange),
      productivityMetrics: await this.calculateProductivityMetrics(timeRange)
    };
    this.analyticsCache.set(cacheKey, metrics);
    return metrics;
  }
  /**
   * Calculate team-level metrics
   */
  async calculateTeamMetrics(timeRange) {
    const allMembers = Array.from(this.teamMembers.values());
    const activeMembers = allMembers.filter(
      (m) => m.lastActive >= timeRange.start && m.lastActive <= timeRange.end
    );
    const newMembers = allMembers.filter(
      (m) => m.joinedAt >= timeRange.start && m.joinedAt <= timeRange.end
    );
    const previousPeriodStart = new Date(
      timeRange.start.getTime() - (timeRange.end.getTime() - timeRange.start.getTime())
    );
    const previousActiveMembers = allMembers.filter(
      (m) => m.lastActive >= previousPeriodStart && m.lastActive < timeRange.start
    );
    const retainedMembers = activeMembers.filter(
      (m) => previousActiveMembers.find((pm) => pm.userId === m.userId)
    );
    const retentionRate = previousActiveMembers.length > 0 ? retainedMembers.length / previousActiveMembers.length : 1;
    return {
      totalMembers: allMembers.length,
      activeMembers: activeMembers.length,
      newMembers: newMembers.length,
      retentionRate
    };
  }
  /**
   * Calculate handoff-specific metrics
   */
  async calculateHandoffMetrics(timeRange) {
    const relevantHandoffs = this.handoffHistory.filter((h) => {
      return true;
    });
    const successfulHandoffs = relevantHandoffs.filter(
      (h) => h.status === "completed"
    );
    const rejectedHandoffs = relevantHandoffs.filter(
      (h) => h.status === "failed"
    );
    const averageApprovalTime = this.calculateAverageTime(
      successfulHandoffs,
      "approval"
    );
    const handoffPairs = this.analyzeHandoffPairs(relevantHandoffs);
    return {
      totalHandoffs: relevantHandoffs.length,
      successfulHandoffs: successfulHandoffs.length,
      averageApprovalTime,
      rejectionRate: relevantHandoffs.length > 0 ? rejectedHandoffs.length / relevantHandoffs.length : 0,
      mostActiveHandoffPairs: handoffPairs
    };
  }
  /**
   * Calculate conflict resolution metrics
   */
  async calculateConflictMetrics(timeRange) {
    const relevantMerges = this.mergeHistory.filter((m) => {
      return m.startedAt >= timeRange.start && m.startedAt <= timeRange.end;
    });
    const totalConflicts = relevantMerges.reduce(
      (sum, m) => sum + m.conflicts.length,
      0
    );
    const autoResolved = relevantMerges.reduce(
      (sum, m) => sum + m.metadata.autoResolvedConflicts,
      0
    );
    const manualResolved = relevantMerges.reduce(
      (sum, m) => sum + m.metadata.manualResolvedConflicts,
      0
    );
    const averageResolutionTime = this.calculateAverageResolutionTime(relevantMerges);
    const conflictHotspots = this.analyzeConflictHotspots(relevantMerges);
    return {
      totalConflicts,
      autoResolvedConflicts: autoResolved,
      manualResolvedConflicts: manualResolved,
      averageResolutionTime,
      conflictHotspots
    };
  }
  /**
   * Calculate productivity metrics
   */
  async calculateProductivityMetrics(timeRange) {
    return {
      frameCreationRate: 15.5,
      // frames per day
      frameCompletionRate: 12.3,
      // frames per day
      averageFrameLifecycle: 2.5,
      // days
      collaborativeFramePercentage: 0.65
      // 65% of frames involve collaboration
    };
  }
  /**
   * Generate actionable insights from metrics
   */
  async generateInsights(metrics) {
    const insights = [];
    if (metrics.handoffMetrics.rejectionRate > 0.3) {
      insights.push({
        id: `high-rejection-${Date.now()}`,
        type: "risk",
        severity: "high",
        title: "High Handoff Rejection Rate",
        description: `${Math.round(metrics.handoffMetrics.rejectionRate * 100)}% of handoffs are being rejected`,
        actionItems: [
          "Review handoff quality guidelines",
          "Implement pre-handoff checklists",
          "Provide additional training on code review standards"
        ],
        affectedUsers: [],
        // Would identify specific users
        metrics: { rejectionRate: metrics.handoffMetrics.rejectionRate },
        generatedAt: /* @__PURE__ */ new Date()
      });
    }
    if (metrics.handoffMetrics.averageApprovalTime > 24 * 60 * 60 * 1e3) {
      insights.push({
        id: `slow-approval-${Date.now()}`,
        type: "opportunity",
        severity: "medium",
        title: "Slow Handoff Approval Times",
        description: "Average approval time exceeds 24 hours",
        actionItems: [
          "Set up approval time SLAs",
          "Implement automated reminders",
          "Add more reviewers to distribute load"
        ],
        affectedUsers: [],
        metrics: {
          averageApprovalTime: metrics.handoffMetrics.averageApprovalTime
        },
        generatedAt: /* @__PURE__ */ new Date()
      });
    }
    if (metrics.conflictMetrics.manualResolvedConflicts > metrics.conflictMetrics.autoResolvedConflicts) {
      insights.push({
        id: `high-conflicts-${Date.now()}`,
        type: "risk",
        severity: "medium",
        title: "High Manual Conflict Resolution",
        description: "Most conflicts require manual resolution",
        actionItems: [
          "Review merge policies to increase auto-resolution",
          "Improve frame naming and organization conventions",
          "Implement conflict prevention guidelines"
        ],
        affectedUsers: [],
        metrics: {
          manualConflicts: metrics.conflictMetrics.manualResolvedConflicts,
          autoConflicts: metrics.conflictMetrics.autoResolvedConflicts
        },
        generatedAt: /* @__PURE__ */ new Date()
      });
    }
    if (metrics.productivityMetrics.collaborativeFramePercentage > 0.8) {
      insights.push({
        id: `high-collaboration-${Date.now()}`,
        type: "success",
        severity: "low",
        title: "Excellent Collaboration Rate",
        description: `${Math.round(metrics.productivityMetrics.collaborativeFramePercentage * 100)}% of frames involve collaboration`,
        actionItems: [
          "Document successful collaboration patterns",
          "Share best practices with other teams",
          "Consider mentoring programs"
        ],
        affectedUsers: [],
        metrics: {
          collaborationRate: metrics.productivityMetrics.collaborativeFramePercentage
        },
        generatedAt: /* @__PURE__ */ new Date()
      });
    }
    return insights;
  }
  /**
   * Identify workflow patterns from team activity
   */
  async identifyWorkflowPatterns() {
    const patterns = [];
    patterns.push({
      id: "feature-development",
      name: "Feature Development Workflow",
      description: "Individual development \u2192 Review \u2192 Merge to shared stack",
      frequency: this.calculatePatternFrequency("feature-development"),
      participants: this.getPatternParticipants("feature-development"),
      steps: [
        {
          action: "Create individual frames",
          averageTime: 4 * 60 * 60 * 1e3,
          successRate: 0.95
        },
        {
          action: "Request handoff",
          averageTime: 5 * 60 * 1e3,
          successRate: 0.9
        },
        {
          action: "Review and approve",
          averageTime: 2 * 60 * 60 * 1e3,
          successRate: 0.85
        },
        {
          action: "Merge to shared stack",
          averageTime: 10 * 60 * 1e3,
          successRate: 0.95
        }
      ],
      efficiency: 0.85,
      recommendations: [
        "Standardize review criteria to reduce rejection rate",
        "Implement automated checks before handoff request"
      ]
    });
    patterns.push({
      id: "pair-programming",
      name: "Pair Programming Workflow",
      description: "Direct collaboration in shared stack",
      frequency: this.calculatePatternFrequency("pair-programming"),
      participants: this.getPatternParticipants("pair-programming"),
      steps: [
        {
          action: "Create shared stack",
          averageTime: 2 * 60 * 1e3,
          successRate: 0.99
        },
        {
          action: "Collaborative development",
          averageTime: 6 * 60 * 60 * 1e3,
          successRate: 0.9
        },
        {
          action: "Resolve conflicts in real-time",
          averageTime: 15 * 60 * 1e3,
          successRate: 0.95
        }
      ],
      efficiency: 0.92,
      recommendations: [
        "Excellent pattern - consider promoting for complex features",
        "Document best practices for real-time collaboration"
      ]
    });
    return patterns;
  }
  /**
   * Get team performance dashboard data
   */
  async getDashboardData(timeRange) {
    const metrics = await this.generateCollaborationMetrics(timeRange);
    const insights = await this.generateInsights(metrics);
    const patterns = await this.identifyWorkflowPatterns();
    const topCollaborators = this.calculateTopCollaborators(timeRange);
    return {
      metrics,
      insights,
      patterns,
      topCollaborators
    };
  }
  // Helper methods
  invalidateCache(type) {
    const keysToRemove = Array.from(this.analyticsCache.keys()).filter(
      (key) => key.includes(type)
    );
    keysToRemove.forEach((key) => this.analyticsCache.delete(key));
  }
  calculateAverageTime(items, type) {
    return 2 * 60 * 60 * 1e3;
  }
  analyzeHandoffPairs(handoffs) {
    return [
      { source: "alice", target: "bob", count: 15, successRate: 0.87 },
      { source: "bob", target: "charlie", count: 12, successRate: 0.92 }
    ];
  }
  calculateAverageResolutionTime(merges) {
    const completedMerges = merges.filter((m) => m.completedAt && m.startedAt);
    if (completedMerges.length === 0) return 0;
    const totalTime = completedMerges.reduce(
      (sum, m) => sum + (m.completedAt.getTime() - m.startedAt.getTime()),
      0
    );
    return totalTime / completedMerges.length;
  }
  analyzeConflictHotspots(merges) {
    return [
      { frameType: "implementation", conflictType: "content", frequency: 25 },
      { frameType: "task", conflictType: "metadata", frequency: 15 },
      { frameType: "test", conflictType: "sequence", frequency: 10 }
    ];
  }
  calculatePatternFrequency(patternId) {
    return Math.random() * 50;
  }
  getPatternParticipants(patternId) {
    return Array.from(this.teamMembers.keys()).slice(0, 3);
  }
  calculateTopCollaborators(timeRange) {
    return Array.from(this.teamMembers.entries()).map(([userId, member]) => ({
      userId,
      handoffsInitiated: Math.floor(Math.random() * 20),
      handoffsApproved: Math.floor(Math.random() * 15),
      conflictsResolved: Math.floor(Math.random() * 10),
      collaborationScore: Math.random()
    })).sort((a, b) => b.collaborationScore - a.collaborationScore);
  }
}
export {
  TeamAnalytics
};
//# sourceMappingURL=team-analytics.js.map
