import type { SetupPlugin, SetupTemplateDefinition } from '../plugin-types';
import { CRM_TEMPLATE_FILES } from '../installation-profile';
import { crmInstallationProfiles } from './crm-installation-profile';

const CRM_TEMPLATE_ORDER = new Map([
  ['master-options.csv', 6],
  ['branches.csv', 7],
  ['product-types.csv', 8],
  ['document-sequences.csv', 14],
  ['approval-workflows.csv', 15],
  ['approval-steps.csv', 16],
  ['approval-matrix.csv', 17],
  ['customers.csv', 30],
  ['contacts.csv', 32],
  ['contact-shares.csv', 33],
  ['leads.csv', 34],
  ['opportunities.csv', 36],
  ['opportunity-parties.csv', 37],
  ['quotations.csv', 40],
  ['quotation-items.csv', 41],
  ['quotation-parties.csv', 42],
  ['quotation-topics.csv', 43],
  ['quotation-topic-items.csv', 44]
]);

export class CrmSetupPlugin implements SetupPlugin {
  id = 'crm';
  version = '1.0.0';
  displayName = 'CRM';
  description = 'CRM setup templates, verification checks, reset scopes, and golden dataset registration.';
  dependencies = ['foundation'];

  installationProfiles() {
    return crmInstallationProfiles;
  }

  templates(): SetupTemplateDefinition[] {
    return CRM_TEMPLATE_FILES.map((file) => ({
      file,
      seedType: CRM_TEMPLATE_ORDER.get(file)! < 30 ? 'foundation' : 'business',
      importOrder: CRM_TEMPLATE_ORDER.get(file) ?? 999,
      supported: true,
      requiredColumns: [],
      description: `CRM setup template: ${file}`,
      plugin: this.id,
      profiles: ['crm_starter', 'crm_demo', 'production', 'custom']
    }));
  }

  wizardSteps() {
    return [
      {
        key: 'crm-templates',
        title: 'CRM Templates',
        description: 'CRM-owned template pack and import order.',
        required: true,
        produces: ['crm_template_registry'],
        invalidates: ['csv_preview']
      },
      {
        key: 'crm-verification',
        title: 'CRM Verification',
        description: 'CRM authorization, sequence, approval, PDF, report, and notification checks.',
        required: true,
        produces: ['crm_verification_report'],
        invalidates: []
      }
    ];
  }

  readinessChecks() {
    return [
      {
        id: 'PLUGIN_CRM_REGISTERED',
        area: 'Plugin registry',
        name: 'CRM plugin registered',
        requiredFor: ['CRM setup'],
        purpose: 'Ensure CRM setup capabilities are available.'
      }
    ];
  }

  verificationChecks() {
    return [
      {
        id: 'PLUGIN_CRM_VERIFICATION',
        area: 'Plugin registry',
        name: 'CRM verification registered',
        requiredFor: ['CRM setup'],
        purpose: 'Ensure CRM verification checks are exposed through the plugin registry.'
      }
    ];
  }

  previewValidators() {
    return [
      { key: 'crm-reference-validator', description: 'CRM customer, lead, opportunity, quotation reference validation.' }
    ];
  }

  importHandlers() {
    return [{ key: 'crm-csv-importer', description: 'CRM setup CSV import handlers.' }];
  }

  resetScopes() {
    return [
      {
        key: 'business_reset',
        destructive: true,
        allowedEnvironments: ['local', 'test', 'staging'],
        requiresTypedConfirmation: true,
        affectedTables: ['crm_customers', 'crm_leads', 'crm_opportunities', 'crm_quotations'],
        storageCleanup: 'best_effort' as const
      },
      {
        key: 'demo_uat_reset',
        destructive: true,
        allowedEnvironments: ['local', 'test', 'staging'],
        requiresTypedConfirmation: true,
        affectedTables: ['crm_* demo rows'],
        storageCleanup: 'best_effort' as const
      },
      {
        key: 'csv_import_rollback',
        destructive: true,
        allowedEnvironments: ['local', 'test', 'staging'],
        requiresTypedConfirmation: true,
        affectedTables: ['manifest-owned rows'],
        storageCleanup: 'report_only' as const
      }
    ];
  }

  reseedTargets() {
    return [
      { key: 'demo_uat', description: 'CRM demo/UAT reseed target.', executable: false },
      { key: 'csv_manifest', description: 'CRM CSV manifest replay target.', executable: false }
    ];
  }

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