import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
} from '@angular/core';
import {
  ReturnProcess,
  ReturnProcessQuestion,
  ReturnProcessQuestionType,
  ReturnProcessService,
  ReturnProcessStore,
} from '@isa/oms/data-access';
import { ReturnProcessQuestionsRendererComponent } from './return-process-questions-renderer/return-process-questions-renderer';

/**
 * Component for managing return process questions.
 *
 * This component handles:
 * - Managing return process questions and answers
 * - Displaying appropriate question types based on product category
 * - Updating question state based on previous answers
 *
 * @example
 * ```html
 * <oms-feature-return-process-questions
 *   [returnProcessId]="processId"
 * ></oms-feature-return-process-questions>
 * ```
 */
@Component({
  selector: 'oms-feature-return-process-questions',
  templateUrl: './return-process-questions.component.html',
  styleUrls: ['./return-process-questions.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ReturnProcessQuestionsRendererComponent],
})
export class ReturnProcessQuestionsComponent {
  #returnProcessStore = inject(ReturnProcessStore);
  // Renamed service to match naming conventions
  #returnProcessService = inject(ReturnProcessService);

  ReturnProcessQuestionType = ReturnProcessQuestionType;

  returnProcessId = input.required<number>();

  returnProcess = computed<ReturnProcess | undefined>(() => {
    const processId = this.returnProcessId();
    return this.#returnProcessStore.entityMap()[processId];
  });

  // Provide stronger typing if `activeReturnProcessQuestions` returns an array
  questions = computed<ReturnProcessQuestion[]>(() => {
    const currentProcess = this.returnProcess();
    if (!currentProcess) {
      // Provide an empty array fallback
      return [];
    }

    if (
      !currentProcess?.productCategory ||
      currentProcess.productCategory === 'unknown'
    ) {
      return [];
    }

    return (
      this.#returnProcessService.activeReturnProcessQuestions(currentProcess) ??
      []
    );
  });
}
