import { AbstractDialog } from '@/shared/abstractClasses/abstract-dialog';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { Component, effect, EventEmitter, Input, Output, signal } from '@angular/core';
import { Step, StepList, Stepper } from 'primeng/stepper';
import { IComplexResponse, IPosResponse } from '../../../models';
import { ConsumerPosFormContentComponent } from '../../poses/form.component';
import { ConsumerComplexFormContentComponent } from '../form.component';

export interface ICreateComplexStepperResult {
  complex: IComplexResponse;
  pos: IPosResponse;
}

@Component({
  selector: 'partner-consumer-complexes-create-wrapper',
  templateUrl: './create-wrapper.component.html',
  imports: [
    SharedDialogComponent,
    Stepper,
    StepList,
    Step,
    ConsumerComplexFormContentComponent,
    ConsumerPosFormContentComponent,
  ],
})
export class ConsumerComplexesCreateWrapperComponent extends AbstractDialog {
  @Input({ required: true }) consumerId!: string;
  @Input({ required: true }) businessActivityId!: string;
  @Output() onSubmit = new EventEmitter<ICreateComplexStepperResult>();

  activeStep = signal(1);
  complex = signal<IComplexResponse | null>(null);

  constructor() {
    super();

    effect(() => {
      if (this.visible) {
        this.resetWizard();
      }
    });
  }

  onComplexSubmit(response: IComplexResponse) {
    this.complex.set(response);
    this.activeStep.set(2);
  }

  onPosSubmit(response: IPosResponse) {
    const complex = this.complex();
    if (!complex) {
      return;
    }

    this.onSubmit.emit({
      complex,
      pos: response,
    });

    this.close();
  }

  override close() {
    this.resetWizard();
    super.close();
  }

  private resetWizard() {
    this.activeStep.set(1);
    this.complex.set(null);
  }
}
