import { nationalIdValidator } from '@/core/validators/national-id.validator';
import { AbstractForm } from '@/shared/abstractClasses';
import { InputComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { CustomerType } from '@/shared/localEnum/constants/customerTypes';
import { Component, inject } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { Observable } from 'rxjs';
import { IUnknownCustomer } from '../../../models/customer';
import { PosLandingStore } from '../../../store/main.store';

@Component({
  selector: 'pos-customer-unknown-form',
  templateUrl: './form.component.html',
  imports: [ReactiveFormsModule, InputComponent, FormFooterActionsComponent],
})
export class CustomerUnknownFormComponent extends AbstractForm<IUnknownCustomer, IUnknownCustomer> {
  private readonly store = inject(PosLandingStore);

  override showSuccessMessage = false;
  form = this.fb.group({
    national_id: [
      this.store?.customer().info?.customer_unknown?.national_id || '',
      [nationalIdValidator()],
    ],
    name: [this.store?.customer().info?.customer_unknown?.name || ''],
  });

  override submitForm() {
    return new Observable<IUnknownCustomer>((observer) => {
      const info = this.form.value as IUnknownCustomer;
      this.store.setCustomer({
        customer_id: '',
        type: CustomerType.UNKNOWN,
        info: {
          customer_unknown: info,
        },
      });
      return observer.next(info);
    });
  }
}
