import { postalCodeValidator } from '@/core/validators';
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 { ILegalCustomer } from '../../../models/customer';
import { PosLandingStore } from '../../../store/main.store';

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

  override showSuccessMessage = false;

  form = this.fb.group({
    company_name: [
      this.store.customer().info?.customer_legal?.company_name || '',
      [Validators.required],
    ],
    registration_number: [
      this.store.customer().info?.customer_legal?.registration_number || '',
      [Validators.required],
    ],
    economic_code: [
      this.store.customer().info?.customer_legal?.economic_code || '',
      [Validators.required],
    ],
    postal_code: [
      this.store.customer().info?.customer_legal?.postal_code || '',
      [postalCodeValidator()],
    ],
  });

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