import { SetupWizardStepConfig, StepValidationResult } from '../../setup-wizard/step-config';
import { settingsStore } from '../../store';
import log from 'electron-log';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

/**
 * Career Wizard steps registry
 * These steps guide users through career-specific setup
 */
export const CAREER_WIZARD_STEPS: SetupWizardStepConfig[] = [
  // Step 1: Canvas Setup Prerequisite Check
  {
    id: 'canvas-setup-check',
    title: 'Canvas Setup',
    description: 'Ensure Canvas Setup is complete before proceeding',
    componentName: 'CanvasSetupCheckStep',
    order: 0,
    required: true,
    validate: async (): Promise<StepValidationResult> => {
      try {
        const canvasState = settingsStore.get('onboarding');

        // Check if Canvas Setup is complete
        const allComplete = canvasState.completed;

        log.debug('Canvas Setup check:', {
          completed: allComplete,
          stepStatus: canvasState.stepStatus
        });

        return {
          isComplete: allComplete,
          canProceed: true, // Can always view this step
          message: allComplete
            ? 'Canvas Setup is complete'
            : 'Canvas Setup must be completed first',
          details: {
            canvasCompleted: canvasState.completed,
            canvasStepStatus: canvasState.stepStatus
          }
        };
      } catch (error: any) {
        log.error('Error validating canvas-setup-check:', error);
        return {
          isComplete: false,
          canProceed: true,
          message: 'Error checking Canvas Setup status'
        };
      }
    },
    icon: 'CheckCircle2'
  },

  // Step 2: Git Clone Career Repositories
  {
    id: 'career-git-clone',
    title: 'Git Clone',
    description: 'Clone career development repositories',
    componentName: 'CareerGitCloneStep',
    order: 1,
    required: true,
    validate: async (): Promise<StepValidationResult> => {
      try {
        const { checkCareerRepositoriesStatus } = require('../../services/career-manager');
        const careerState = settingsStore.get('careerSetup');
        const stepData = careerState.stepStatus['career-git-clone'];
        const baseDir = stepData?.data?.baseDirectory;

        log.debug('Career git clone validation:', {
          baseDir,
          stepData
        });

        const status = await checkCareerRepositoriesStatus(baseDir);

        log.debug('Career repositories status:', {
          configured: status.configured,
          canvasHorizon: status.canvasHorizon.isValidRepo,
          journey: status.journey.isValidRepo,
          configExists: status.configExists
        });

        return {
          isComplete: status.configured,
          canProceed: true,
          message: status.configured
            ? `Career repositories configured at ${status.baseDirectory}`
            : status.baseDirectory
            ? 'Repositories need to be cloned'
            : 'No folder selected',
          details: {
            baseDirectory: status.baseDirectory,
            canvasHorizon: status.canvasHorizon,
            journey: status.journey,
            configExists: status.configExists,
            configured: status.configured
          }
        };
      } catch (error: any) {
        log.error('Error validating career-git-clone:', error);
        return {
          isComplete: false,
          canProceed: true,
          message: 'Error checking repository status'
        };
      }
    },
    icon: 'GitBranch'
  }
];

/**
 * Get all career wizard steps
 */
export function getCareerWizardSteps(): SetupWizardStepConfig[] {
  return CAREER_WIZARD_STEPS;
}
