/**
 * Static text and structure of QRS Form 04-2a, copied from the Word reports.
 * Change wording here, not in the template, so Stage 1 / Stage 2 stay consistent.
 */
import type { StageType, Conclusion, Stage1Recommendation } from './entities/audit-report.entity';

export const STAGE_LABEL: Record<StageType, string> = {
  stage1: 'Stage 1 (Initial)',
  stage2: 'Stage 2 (Initial)',
  surveillance: 'Surveillance',
  recert: 'Recertification',
};

/** Which clause_library scope a stage uses. Stage 1 = readiness list; everything else = full clause list. */
export const LIBRARY_SCOPE: Record<StageType, 'readiness' | 'full'> = {
  stage1: 'readiness',
  stage2: 'full',
  surveillance: 'full',
  recert: 'full',
};

/** Report-number prefix per stage, e.g. S1-2026-000123 */
export const REPORT_NO_PREFIX: Record<StageType, string> = {
  stage1: 'S1',
  stage2: 'S2',
  surveillance: 'SV',
  recert: 'RC',
};

export const CONCLUSION_OPTIONS: { key: Conclusion; text: string }[] = [
  { key: 'complies', text: 'The management system complies with the requirements of the reference standard — recommend Issuance / Continuation of Certificate.' },
  { key: 'minor_nc', text: 'Complies with exception of minor NC(s) — recommend Issuance / Continuation of Certificate upon off-site verification of closure of all minor NCs within 60 days from the date of audit.' },
  { key: 'major_nc', text: 'Evidence of major nonconformity(ies) — not recommended for Issuance / Continuation of Certificate at this time; follow-up audit required within 60 days to verify on-site closure.' },
  { key: 'not_recommended', text: 'Not recommended — organization has not implemented / maintained the system/process at pace; full audit required.' },
];

export const STAGE1_RECOMMENDATION_OPTIONS: { key: Stage1Recommendation; text: string }[] = [
  { key: 'proceed', text: "Recommended proceeding with Stage 2 (within 60 days from this audit date)." },
  { key: 'hold', text: "Recommend not proceeding to Stage 2 until audit evidence has been submitted showing that the concerns raised by the auditor(s) have been rectified. A date for Stage 2 will then be agreed." },
  { key: 'repeat_stage1', text: "Recommend not proceeding without a further Stage 1 audit due to the severity of the concerns raised by the audit team." },
];

export const FINDING_CATEGORIES: { category: string; classification: string; definition: string }[] = [
  {
    "category": "Category 1",
    "classification": "Major Non-Conformity",
    "definition": "A nonconformity which affects the capability of the management system to achieve its intended results, raises significant doubt on the effectiveness of the QMS, could result in harm to the organization's ability to consistently provide conforming products/services, or reflects the recurrence/persistence of an unresolved Minor Non-Conformity. Corrective action is mandatory."
  },
  {
    "category": "Category 2",
    "classification": "Minor Non-Conformity",
    "definition": "A nonconformity which does not affect the capability of the management system to achieve its intended results, but reflects a single lapse or partial breakdown in the implementation of a documented process or requirement. Corrective action is mandatory."
  },
  {
    "category": "Category 3",
    "classification": "Observation",
    "definition": "Requirements and expectations are generally met, with minor inadequacies or omissions noted; in the auditor's opinion, preventive action would improve the effectiveness of the implemented management system. Corrective action is not mandatory."
  },
  {
    "category": "Category 4",
    "classification": "Satisfactory / Conformity",
    "definition": "The requirement is fully met and effectively implemented."
  },
  {
    "category": "Category 5",
    "classification": "Positive Point",
    "definition": "A point worth noting as good practice or a particular strength, which the organization is encouraged to continue and build upon."
  }
];

export const DISCLAIMER = "This report has been prepared by Quality Registrar System in respect of the organization's application for assessment. The purpose of this report is to record evidence of the organization's compliance with the audited standard(s). Quality Registrar System does not warrant the suitability of the contents of this report for any particular purpose, and accepts no liability for consequences arising from actions taken by third parties based on this report. The findings recorded are the result of a sample-based audit; the absence of a recorded nonconformity does not imply that no other nonconformities exist. The organization is required to implement corrective action(s) should further nonconformities be identified. This report is confidential, and its distribution is limited to the audit team, the relevant accreditation / competent authority, the organization, and the Quality Registrar System.";

export const CONFIDENTIALITY =
  'This report and its contents are confidential to the client organization and Quality Registrar Systems. It shall not be reproduced or disclosed to any third party, in whole or in part, without prior written consent, except as required by accreditation or regulatory bodies.';

/** Team leader declaration. Keys are stored in extras_json.declarations. */
export const DECLARATIONS: { key: string; text: string }[] = [
  {
    "key": "d1",
    "text": "Auditing is based on a sampling process of the available information."
  },
  {
    "key": "d2",
    "text": "Audit is combined, joint, or integrated (delete as applicable)."
  },
  {
    "key": "d3",
    "text": "The effectiveness of corrective actions taken regarding previously identified nonconformities has been verified."
  },
  {
    "key": "d4",
    "text": "Outcomes are effective and compliant."
  },
  {
    "key": "d5",
    "text": "The internal audit and management review processes are effective and comply with the requirements."
  },
  {
    "key": "d6",
    "text": "The scope of certification is appropriate."
  },
  {
    "key": "d7",
    "text": "The capability of the management system to meet applicable requirements and expected outcomes has been confirmed."
  },
  {
    "key": "d8",
    "text": "The audit objectives have been fulfilled and achieved."
  }
];

export const ATTACHMENTS: { key: string; text: string }[] = [
  {
    "key": "a1",
    "text": "Audit Plan"
  },
  {
    "key": "a2",
    "text": "Attendance Sheet"
  },
  {
    "key": "a3",
    "text": "Confidentiality Statement"
  },
  {
    "key": "a4",
    "text": "Company Trade License and Compliance Documents"
  },
  {
    "key": "a5",
    "text": "Audit objectives and Evidence"
  }
];

export const MARK_USE_OPTIONS: { key: string; text: string }[] = [
  {
    "key": "m1",
    "text": "The certification mark/logo is being used by the organization in accordance with Quality Registrar System's approved specimen and conditions of use."
  },
  {
    "key": "m2",
    "text": "The certification mark/logo has not yet been used by the organization; the organization has been made aware of the applicable conditions of use."
  },
  {
    "key": "m3",
    "text": "Incorrect/unauthorized use of the certification mark/logo was identified — refer to Non-Conformity No. ______."
  }
];

export const CHANGE_DETAIL_QUESTIONS: { key: string; label: string; kind: 'yesno' | 'nonespecify' }[] = [
  { key: 'man_days_adequate', label: 'Are quoted man-days adequate?', kind: 'yesno' },
  { key: 'employee_change', label: 'Any change in employee details?', kind: 'nonespecify' },
  { key: 'scope_change', label: 'Any change in scope?', kind: 'nonespecify' },
  { key: 'additional_info', label: 'Any additional information?', kind: 'nonespecify' },
];

export const PREVIOUS_NC_DEFAULT = "During the audit, the non-conformities and observations from the previous audit were reviewed and verified with the auditee.";

/**
 * Free-text sections. 'stages' lists where the section is printed.
 * Order here is the order the editor shows them.
 */
export const SECTION_DEFS: { key: string; label: string; stages: StageType[] | 'all' }[] = [
  { key: 'objective', label: 'Audit objective / purpose', stages: 'all' },
  { key: 'criteria', label: 'Audited criteria', stages: 'all' },
  { key: 'org_brief', label: 'Brief about the organization', stages: 'all' },
  { key: 'audit_brief', label: 'Audit brief and facts', stages: 'all' },
  { key: 'prev_nc', label: 'NCs and corrective action in previous assessment', stages: ['stage2', 'surveillance', 'recert'] },
  { key: 'effectiveness', label: "Auditor's overall conclusion (effectiveness and summary)", stages: ['stage2', 'surveillance', 'recert'] },
  { key: 'improvement_comments', label: 'Audit team comments (continual improvement)', stages: ['stage2', 'surveillance', 'recert'] },
];

export const sectionAppliesTo = (key: string, stage: StageType): boolean => {
  const d = SECTION_DEFS.find((x) => x.key === key);
  return !!d && (d.stages === 'all' || d.stages.includes(stage));
};

export const BRIEF_COMMON = [
  "The audit was carried out based on a sampling of available records, processes, and personnel. The auditee(s) were cooperative throughout the audit, and the scope of the audit was discussed and confirmed with the organization at the opening meeting.",
  "Random sampling was used in gathering audit evidence; this implies certain limitations. Inconsistency or unavailability of a specific piece of documented information does not necessarily indicate that other evidence of conformity does not exist. However, where information, records, or documentation required to evidence conformity to a mandatory requirement were unavailable at the time of audit, this has been recorded as a nonconformity to be addressed through corrective action."
];
export const BRIEF_AREAS = [
  "In an ISO audit, various aspects of an organization's operations to ensure compliance with the specific ISO standard under consideration were assessed. Here are some common areas that the auditor has focused on:",
  "Organizational Context: The auditor examined how well the organization understands and defines its internal and external context. This involves considering factors such as the industry, regulatory environment, and stakeholder expectations.",
  "Leadership and Management: The auditor assessed the effectiveness of the Organization's leadership in establishing a quality management system and demonstrating commitment to meeting the requirements of the ISO standard.",
  "Policy and Objectives: Companies are expected to have documented policies and objectives that align with the ISO standard. The auditor reviewed these documents and assessed their relevance and effectiveness.",
  "Risk Management: ISO standards often require organizations to identify and manage risks. The auditor evaluated how well an organization identifies, assesses, and addresses risks within its processes.",
  "Process Documentation: The auditor reviewed documented processes to ensure they are in line with the ISO standard's requirements. This includes procedures for key activities related to the standard.",
  "Monitoring and Measurement: Companies are expected to monitor and measure various aspects of their operations. The auditor assessed the effectiveness of these processes and the accuracy of the data collected.",
  "Corrective and Preventive Actions: The auditor checked whether the organization has processes in place to identify and address nonconformities, including corrective and preventive actions.",
  "Records and Documentation: The auditor reviewed records to ensure that the organization maintains appropriate documentation and records as required by the ISO standard.",
  "Training and Competence: The competence of personnel is assessed to ensure that employees have the necessary skills and training to carry out their roles in a manner that aligns with ISO requirements.",
  "Communication: The auditor evaluated how effectively the organization communicates both internally and externally about its quality or environmental management system."
];

export const ISO9001_TEXT = {
  stage1: { objective: "To determine the readiness of the organization's Quality Management System (QMS) for Stage 2 Certification by evaluating documented information, understanding the organization's context, scope, quality policy, legal and other requirements, quality objectives and planning processes, and the establishment of the management system in accordance with ISO 9001:2015.", criteria: "Organization context and interested parties, scope of the QMS, Quality Management System documented information, quality policy, actions to address risks and opportunities, quality objectives and planning to achieve them, legal and other requirements, customer and product/service requirements, operational planning and control, monitoring and measurement processes, competence and awareness, documented procedures and records, and the organization's readiness for Stage 2 Certification Audit in accordance with ISO 9001:2015." },
  full: { objective: "To verify the effective implementation, maintenance, and effectiveness of the Quality Management System (QMS) in accordance with ISO 9001:2015, evaluate conformity with the standard requirements, verify compliance with applicable statutory, regulatory and customer requirements, assess the achievement of quality objectives and planned results, and confirm continual improvement of the QMS to support certification.", criteria: "Organization context and interested parties, QMS and associated processes, quality policy, actions to address risks and opportunities, quality objectives and action plans, compliance with applicable statutory, regulatory and customer requirements, operational planning and control, requirements for products and services, design and development, control of externally provided processes/products/services, production and service provision, competence and awareness, communication, monitoring, measurement, analysis and evaluation, customer satisfaction, performance evaluation, internal audit, management review, nonconformity and corrective actions, continual improvement, documented information (manual, procedures, records, and related documents), and verification of the effective implementation and effectiveness of the QMS in accordance with ISO 9001:2015." },
};

/** defaultSectionBody() and the per-standard wording now live in report.standards.ts */
