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

export const LEARNER_HUB_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');
        const allComplete = canvasState.completed;

        return {
          isComplete: allComplete,
          canProceed: true,
          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 (learner-hub):', error);
        return {
          isComplete: false,
          canProceed: true,
          message: 'Error checking Canvas Setup status'
        };
      }
    },
    icon: 'CheckCircle2'
  },

  // Step 2: Git Clone Learner Hub Repository
  {
    id: 'learner-hub-git-clone',
    title: 'Git Clone',
    description: 'Clone the learner-hub repository',
    componentName: 'LearnerHubGitCloneStep',
    order: 1,
    required: true,
    validate: async (): Promise<StepValidationResult> => {
      try {
        const { checkLearnerHubRepositoryStatus } = require('../../services/learner-hub-manager');
        const learnerHubState = settingsStore.get('learnerHubSetup');
        const stepData = learnerHubState.stepStatus['learner-hub-git-clone'];
        const baseDir = stepData?.data?.baseDirectory;

        const status = await checkLearnerHubRepositoryStatus(baseDir);

        return {
          isComplete: status.configured,
          canProceed: true,
          message: status.configured
            ? `Learner Hub repository configured at ${status.baseDirectory}`
            : status.baseDirectory
            ? 'Repository needs to be cloned'
            : 'No folder selected',
          details: {
            baseDirectory: status.baseDirectory,
            learnerHub: status.learnerHub,
            configExists: status.configExists,
            configured: status.configured
          }
        };
      } catch (error: any) {
        log.error('Error validating learner-hub-git-clone:', error);
        return {
          isComplete: false,
          canProceed: true,
          message: 'Error checking repository status'
        };
      }
    },
    icon: 'GitBranch'
  }
];

export function getLearnerHubSteps(): SetupWizardStepConfig[] {
  return LEARNER_HUB_STEPS;
}
