import { Step, StepType } from '../../types';

/**
 * Defines the logical order of pottery steps in the creation process
 */
const STEP_ORDER: StepType[] = [
  'forming',
  'trimming',
  'drying',
  'bisque_firing',
  'glazing',
  'glaze_firing',
  'misc',
];

/**
 * Get the order index for a step type (lower = earlier in process)
 */
export function getStepOrderIndex(stepType: StepType): number {
  const index = STEP_ORDER.indexOf(stepType);
  return index === -1 ? 999 : index;
}

/**
 * Sort steps by their logical order in the pottery process
 * Steps of the same type are sorted by creation date
 */
export function sortStepsByLogicalOrder(steps: Step[]): Step[] {
  return [...steps].sort((a, b) => {
    const orderA = getStepOrderIndex(a.type);
    const orderB = getStepOrderIndex(b.type);

    if (orderA !== orderB) {
      return orderA - orderB;
    }

    // Same step type, sort by creation date
    return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
  });
}

/**
 * Get the next suggested step based on completed steps
 * Returns null if all typical steps are completed
 */
export function suggestNextStep(completedSteps: Step[]): StepType | null {
  if (completedSteps.length === 0) {
    return 'forming';
  }

  // Find the highest step index completed so far
  let highestIndex = -1;
  const completedTypes = new Set(completedSteps.map(s => s.type));

  for (const stepType of completedTypes) {
    const index = getStepOrderIndex(stepType as StepType);
    if (index > highestIndex && index < 999) { // 999 is invalid/misc
      highestIndex = index;
    }
  }

  // Suggest the next step in the sequence
  if (highestIndex !== -1 && highestIndex < STEP_ORDER.length - 1) {
    const nextStepInfo = STEP_ORDER[highestIndex + 1];
    if (nextStepInfo !== 'misc') {
      return nextStepInfo;
    }
  }

  return null;
}

/**
 * Check if steps are in a reasonable order
 * Returns warnings for potentially out-of-order steps
 */
export function validateStepOrder(steps: Step[]): string[] {
  const warnings: string[] = [];
  const stepIndices = steps.map(s => ({ type: s.type, order: getStepOrderIndex(s.type) }));

  // Check for major order violations (e.g., glazing before bisque firing)
  for (let i = 0; i < stepIndices.length - 1; i++) {
    const current = stepIndices[i];
    const next = stepIndices[i + 1];

    // If we go backwards by more than 1 step (allowing for some flexibility)
    if (current.order > next.order + 1) {
      warnings.push(`${current.type} typically comes before ${next.type}`);
    }
  }

  // Specific pottery logic checks
  const hasGlazing = steps.some(s => s.type === 'glazing');
  const hasBisque = steps.some(s => s.type === 'bisque_firing');
  const hasGlazeFiring = steps.some(s => s.type === 'glaze_firing');

  if (hasGlazing && !hasBisque) {
    warnings.push('Glazing typically requires bisque firing first');
  }

  if (hasGlazeFiring && !hasGlazing) {
    warnings.push('Glaze firing typically requires glazing first');
  }

  return warnings;
}
