import {
  FOUNDATION_TEMPLATE_FILES,
  FOUNDATION_WIZARD_STEPS,
  type InstallationProfile
} from '../installation-profile';
import type { SetupPlugin, SetupTemplateDefinition } from '../plugin-types';

const FOUNDATION_PROFILES: InstallationProfile[] = [
  {
    id: 'crm_starter',
    name: 'CRM Starter',
    description: 'Foundation plus CRM structure without demo business volume.',
    wizardSteps: FOUNDATION_WIZARD_STEPS,
    requiredTemplates: FOUNDATION_TEMPLATE_FILES,
    verificationChecks: ['ENV_AUTH_SECRET', 'ENV_DATABASE_URL', 'DB_CONNECTIVITY', 'ADMIN_USER'],
    resetScopes: ['setup_run_reset'],
    reseedTargets: ['foundation'],
    defaultOptions: { importMode: 'strict' },
    enabledPluginIds: ['foundation', 'crm']
  },
  {
    id: 'restore_backup',
    name: 'Restore Backup',
    description: 'Minimal setup checks for restored environments.',
    wizardSteps: ['readiness', 'verification', 'finish'],
    requiredTemplates: [],
    verificationChecks: ['ENV_AUTH_SECRET', 'ENV_DATABASE_URL', 'DB_CONNECTIVITY'],
    resetScopes: ['setup_run_reset'],
    reseedTargets: [],
    defaultOptions: { importMode: 'none' },
    enabledPluginIds: ['foundation']
  },
  {
    id: 'custom',
    name: 'Custom',
    description: 'Operator-selected setup modules and templates.',
    wizardSteps: FOUNDATION_WIZARD_STEPS,
    requiredTemplates: [],
    verificationChecks: ['ENV_AUTH_SECRET', 'ENV_DATABASE_URL', 'DB_CONNECTIVITY'],
    resetScopes: ['setup_run_reset'],
    reseedTargets: [],
    defaultOptions: { importMode: 'custom' },
    enabledPluginIds: ['foundation']
  }
];

export class FoundationSetupPlugin implements SetupPlugin {
  id = 'foundation';
  version = '1.0.0';
  displayName = 'Setup Foundation';
  description = 'Core setup readiness, setup state, seed manifests, reset framework, and CSV framework.';
  dependencies: string[] = [];

  installationProfiles() {
    return FOUNDATION_PROFILES;
  }

  templates(): SetupTemplateDefinition[] {
    return [];
  }

  wizardSteps() {
    return [
      {
        key: 'readiness',
        title: 'System Readiness',
        description: 'Run non-destructive environment and platform checks.',
        required: true,
        produces: ['readiness_report'],
        invalidates: ['templates', 'csv_preview', 'csv_commit', 'verification']
      },
      {
        key: 'templates',
        title: 'Template Pack',
        description: 'Review registered setup templates for the selected profile.',
        required: true,
        produces: ['template_registry'],
        invalidates: ['csv_preview']
      },
      {
        key: 'upload',
        title: 'CSV Upload',
        description: 'Select setup CSV files for preview and commit.',
        required: true,
        produces: ['selected_files'],
        invalidates: ['csv_preview', 'csv_commit']
      },
      {
        key: 'preview',
        title: 'CSV Preview',
        description: 'Validate CSVs and produce a preview hash.',
        required: true,
        produces: ['preview_hash'],
        invalidates: ['csv_commit']
      },
      {
        key: 'commit',
        title: 'CSV Commit',
        description: 'Commit validated CSV files and write seed manifests.',
        required: true,
        produces: ['import_report', 'seed_manifest'],
        invalidates: ['verification']
      },
      {
        key: 'verification',
        title: 'Verification',
        description: 'Confirm operational readiness after import.',
        required: true,
        produces: ['verification_report'],
        invalidates: []
      },
      {
        key: 'finish',
        title: 'Finish',
        description: 'Complete the setup run and show summary.',
        required: true,
        produces: ['setup_report'],
        invalidates: []
      }
    ];
  }

  readinessChecks() {
    return [
      {
        id: 'PLUGIN_FOUNDATION_REGISTERED',
        area: 'Plugin registry',
        name: 'Foundation plugin registered',
        requiredFor: ['setup platform'],
        purpose: 'Ensure setup core can resolve foundation capabilities.'
      }
    ];
  }

  verificationChecks() {
    return [
      {
        id: 'PLUGIN_FOUNDATION_VERIFICATION',
        area: 'Plugin registry',
        name: 'Foundation verification registered',
        requiredFor: ['setup platform'],
        purpose: 'Ensure setup verification can resolve foundation plugin checks.'
      }
    ];
  }

  previewValidators() {
    return [{ key: 'template-header-validator', description: 'CSV template header validation.' }];
  }

  importHandlers() {
    return [{ key: 'csv-commit-framework', description: 'Shared setup CSV commit framework.' }];
  }

  resetScopes() {
    return [
      {
        key: 'setup_run_reset',
        destructive: false,
        allowedEnvironments: ['local', 'test', 'staging'],
        requiresTypedConfirmation: true,
        affectedTables: ['setup_runs', 'setup_run_steps', 'seed_manifests', 'seed_manifest_items'],
        storageCleanup: 'none' as const
      }
    ];
  }

  reseedTargets() {
    return [{ key: 'foundation', description: 'Foundation seed service wrapper.', executable: false }];
  }

  goldenDatasets() {
    return [{ key: 'foundation-golden', path: 'setup/golden-dataset', files: FOUNDATION_TEMPLATE_FILES }];
  }
}
