import { postalCodeValidator } from '@/core/validators';
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, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { IIndividualCustomer } from '../../../models/customer';
import { PosLandingStore } from '../../../store/main.store';

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

  override showSuccessMessage = false;
  form = this.fb.group({
    first_name: [
      this.store.customer().info?.customer_individual?.first_name || '',
      [Validators.required],
    ],
    last_name: [
      this.store.customer().info?.customer_individual?.last_name || '',
      [Validators.required],
    ],
    national_id: [
      this.store.customer().info?.customer_individual?.national_id || '',
      [Validators.required, nationalIdValidator()],
    ],
    economic_code: [
      this.store.customer().info?.customer_individual?.economic_code || '',
      [Validators.required],
    ],
    postal_code: [
      this.store.customer().info?.customer_individual?.postal_code || '',
      [postalCodeValidator()],
    ],
  });

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