/**
 * Issue Type Helper Utilities
 * Client-side utilities for working with parent/child issue type relationships
 */

export interface IssueType {
  value: number;
  label: string;
  is_active: boolean;
}

export interface SubIssueType {
  value: number;
  label: string;
  is_active: boolean;
  parent_value?: number | null;
  parent_issue_type_label?: string;
}

export interface TicketWithIssueTypes {
  id: number;
  ticket_number: string;
  title: string;
  issue_type?: number;
  sub_issue_type?: number;
  issue_type_label?: string;
  sub_issue_type_label?: string;
  parent_issue_type_label?: string;
}

/**
 * Get parent issue type for a sub-issue type from cached data
 */
export function getParentIssueType(
  subIssueTypeValue: number,
  subIssueTypes: SubIssueType[]
): string | null {
  const subIssueType = subIssueTypes.find(sit => sit.value === subIssueTypeValue);
  return subIssueType?.parent_issue_type_label || null;
}

/**
 * Filter sub-issue types by parent issue type
 */
export function getSubIssueTypesByParent(
  parentIssueTypeValue: number,
  subIssueTypes: SubIssueType[]
): SubIssueType[] {
  return subIssueTypes.filter(sit => sit.parent_value === parentIssueTypeValue);
}

/**
 * Create a hierarchical tree of issue types and sub-issue types
 */
export function createIssueTypeTree(
  issueTypes: IssueType[],
  subIssueTypes: SubIssueType[]
): Array<IssueType & { subIssues: SubIssueType[] }> {
  return issueTypes.map(issueType => ({
    ...issueType,
    subIssues: getSubIssueTypesByParent(issueType.value, subIssueTypes)
  }));
}

/**
 * Group tickets by parent issue type
 */
export function groupTicketsByParentIssueType(
  tickets: TicketWithIssueTypes[],
  subIssueTypes: SubIssueType[]
): Record<string, TicketWithIssueTypes[]> {
  const groups: Record<string, TicketWithIssueTypes[]> = {};
  
  tickets.forEach(ticket => {
    const parentType = getParentIssueType(ticket.sub_issue_type!, subIssueTypes) || 'Uncategorized';
    
    if (!groups[parentType]) {
      groups[parentType] = [];
    }
    
    groups[parentType].push(ticket);
  });
  
  return groups;
}

/**
 * Get statistics for issue types and sub-issue types
 */
export function getIssueTypeStatistics(
  tickets: TicketWithIssueTypes[],
  subIssueTypes: SubIssueType[]
): {
  totalTickets: number;
  ticketsByParentType: Record<string, number>;
  ticketsBySubType: Record<string, number>;
  unassignedTickets: number;
} {
  const stats = {
    totalTickets: tickets.length,
    ticketsByParentType: {} as Record<string, number>,
    ticketsBySubType: {} as Record<string, number>,
    unassignedTickets: 0
  };

  tickets.forEach(ticket => {
    if (!ticket.sub_issue_type) {
      stats.unassignedTickets++;
      return;
    }

    const parentType = getParentIssueType(ticket.sub_issue_type, subIssueTypes);
    if (parentType) {
      stats.ticketsByParentType[parentType] = (stats.ticketsByParentType[parentType] || 0) + 1;
    }

    const subType = subIssueTypes.find(sit => sit.value === ticket.sub_issue_type);
    if (subType) {
      stats.ticketsBySubType[subType.label] = (stats.ticketsBySubType[subType.label] || 0) + 1;
    }
  });

  return stats;
}

/**
 * Format ticket data for display with parent issue type information
 */
export function formatTicketWithParentIssueType(
  ticket: TicketWithIssueTypes,
  subIssueTypes: SubIssueType[]
): string {
  const parentType = getParentIssueType(ticket.sub_issue_type!, subIssueTypes);
  
  if (parentType) {
    return `${ticket.ticket_number}: ${ticket.title} (${parentType} > ${ticket.sub_issue_type_label})`;
  }
  
  return `${ticket.ticket_number}: ${ticket.title} (${ticket.sub_issue_type_label || 'No sub-issue type'})`;
}

/**
 * Validate that a sub-issue type belongs to a specific parent issue type
 */
export function validateSubIssueTypeParent(
  subIssueTypeValue: number,
  expectedParentValue: number,
  subIssueTypes: SubIssueType[]
): boolean {
  const subIssueType = subIssueTypes.find(sit => sit.value === subIssueTypeValue);
  return subIssueType?.parent_value === expectedParentValue;
}

/**
 * Get all valid parent-child combinations
 */
export function getValidParentChildCombinations(
  issueTypes: IssueType[],
  subIssueTypes: SubIssueType[]
): Array<{
  parentValue: number;
  parentLabel: string;
  childValue: number;
  childLabel: string;
}> {
  const combinations: Array<{
    parentValue: number;
    parentLabel: string;
    childValue: number;
    childLabel: string;
  }> = [];

  issueTypes.forEach(issueType => {
    const children = getSubIssueTypesByParent(issueType.value, subIssueTypes);
    children.forEach(child => {
      combinations.push({
        parentValue: issueType.value,
        parentLabel: issueType.label,
        childValue: child.value,
        childLabel: child.label,
      });
    });
  });

  return combinations;
}
