/**
 * PR-37 Domain Intelligence — Barrel Export
 *
 * Re-exports all modules (Parts B–E) and provides the `createDomainIntelligenceFactory`
 * factory function for pipeline integration.
 *
 * @module pr-37-domain-intelligence
 * @since PR-37
 */

// ══════════════════════════════════════════════════════════
// Imports for factory function
// ══════════════════════════════════════════════════════════

import { classifyProduct } from "./domain-classifier.mjs";
import { generateIndustryEpics } from "./industry-epic-generator.mjs";
import { generateIndustryFeatures } from "./industry-feature-generator.mjs";
import { validateRequirements } from "./domain-validator.mjs";

// ══════════════════════════════════════════════════════════
// Re-exports from Part B — Domain Classifier
// ══════════════════════════════════════════════════════════

export {
  classifyProduct,
  classifyDomain,
  listSupportedDomains,
  fuzzyClassify,
} from "./domain-classifier.mjs";

// ══════════════════════════════════════════════════════════
// Re-exports from Part C — Industry Epic Generator
// ══════════════════════════════════════════════════════════

export {
  generateIndustryEpics,
  resetEpicCounter as resetEpicIdCounter,
  describeDomain,
  listDomainEpics,
} from "./industry-epic-generator.mjs";

// ══════════════════════════════════════════════════════════
// Re-exports from Part D — Industry Feature Generator
// ══════════════════════════════════════════════════════════

export {
  generateIndustryFeatures,
  resetFeatureCounter as resetFeatureIdCounter,
  detectFeatureType,
  FEATURE_TYPE,
} from "./industry-feature-generator.mjs";

// ══════════════════════════════════════════════════════════
// Re-exports from Part E — Domain Validator
// ══════════════════════════════════════════════════════════

export {
  validateRequirements,
  validateEpics,
  validateFeatures,
} from "./domain-validator.mjs";

// ══════════════════════════════════════════════════════════
// Factory Function
// ══════════════════════════════════════════════════════════

/**
 * Factory output shape.
 *
 * @typedef {object} FactoryOutput
 * @property {object} classification - Result from classifyProduct
 * @property {object[]} epics - Generated epics
 * @property {object[]} features - Generated features
 * @property {object} validation - Validation report
 * @property {string[]} warnings - Any warnings encountered
 */

/**
 * Create a domain intelligence factory that runs the full pipeline:
 * classification → epic generation → feature generation → validation.
 *
 * The returned object is compatible with the FactoryOutput contract used
 * by PLM pipeline orchestration.
 *
 * @param {object|string} strategyPackage - Product strategy (object or string)
 * @param {object} [options={}] - Additional options
 * @param {string} [options.domainOverride] - Skip classification and use this domain explicitly
 * @param {boolean} [options.includeValidation=true] - Whether to run validation
 * @returns {FactoryOutput} Complete domain intelligence output
 *
 * @example
 * const factory = createDomainIntelligenceFactory({ productName: "智能仓储WMS", description: "自动化仓库" });
 * console.log(factory.classification.primaryDomain); // "WMS"
 * console.log(factory.epics.length);                 // 7
 * console.log(factory.validation.overallVerdict);    // "pass" | "conditional" | "fail"
 *
 * @example
 * // With explicit domain override
 * const factory = createDomainIntelligenceFactory({ productName: "My Product" }, { domainOverride: "ERP" });
 */
export function createDomainIntelligenceFactory(strategyPackage, options = {}) {
  const {
    domainOverride = null,
    includeValidation = true,
  } = options || {};

  const warnings = [];

  // ── 1. Classification ───────────────────────────────
  let classification;
  let domainName;

  if (domainOverride) {
    domainName = domainOverride;
    classification = {
      primaryDomain: domainName,
      secondaryDomain: null,
      confidenceScore: 1.0,
      matchedKeywords: [],
      classificationReason: `Domain manually overridden to "${domainName}"`,
    };
  } else {
    classification = classifyProduct(strategyPackage);
    domainName = classification.primaryDomain;

    if (!domainName) {
      return {
        classification,
        epics: [],
        features: [],
        validation: null,
        warnings: [
          "Could not classify product into any known domain. Provide a domainOverride or more specific product description.",
        ],
      };
    }

    if (classification.confidenceScore < 0.4) {
      warnings.push(
        `Low confidence (${classification.confidenceScore}) for domain "${domainName}". Consider manual review or domainOverride.`
      );
    }
  }

  // ── 2. Epic Generation ───────────────────────────────
  const epics = generateIndustryEpics(domainName, strategyPackage);

  if (epics.length === 0) {
    warnings.push(
      `No epics generated for domain "${domainName}".`
    );
  }

  // ── 3. Feature Generation ────────────────────────────
  const features = generateIndustryFeatures(domainName, epics);

  if (features.length === 0) {
    warnings.push(
      `No features generated for domain "${domainName}".`
    );
  }

  // ── 4. Validation ────────────────────────────────────
  let validation = null;
  if (includeValidation) {
    validation = validateRequirements(
      { epics, features, userStories: [] },
      domainName
    );
  }

  return {
    classification,
    epics,
    features,
    validation,
    warnings,
  };
}
