import { AbstractFormDialog } from '@/shared/abstractClasses';
import {
  CompanyNameComponent,
  FirstNameComponent,
  LastNameComponent,
  MobileNumberComponent,
  NationalCodeComponent,
  RegistrationNumberComponent,
  SharedDialogComponent,
} from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IProfileRequestPayload, IProfileResponse } from '../models';
import { ProfileService } from '../services/main.service';

@Component({
  selector: 'consumer-profile-form',
  templateUrl: 'form.component.html',
  imports: [
    ReactiveFormsModule,
    FormFooterActionsComponent,
    FirstNameComponent,
    LastNameComponent,
    NationalCodeComponent,
    MobileNumberComponent,
    CompanyNameComponent,
    RegistrationNumberComponent,
    SharedDialogComponent,
  ],
})
export class ConsumerProfileFormComponent extends AbstractFormDialog<
  IProfileRequestPayload,
  IProfileResponse,
  IProfileRequestPayload
> {
  @Input({ required: true }) profileType!: 'INDIVIDUAL' | 'LEGAL';

  private readonly service = inject(ProfileService);

  initForm = () => {
    const form = this.fb.group({
      first_name: fieldControl.first_name(this.initialValues?.first_name),
      last_name: fieldControl.last_name(this.initialValues?.last_name),
      mobile_number: fieldControl.mobile_number(this.initialValues?.mobile_number),
      national_code: fieldControl.national_code(this.initialValues?.national_code),
      company_name: fieldControl.company_name(this.initialValues?.company_name),
      registration_number: fieldControl.registration_number(
        this.initialValues?.registration_number,
      ),
    });

    if (this.profileType === 'LEGAL') {
      // @ts-ignore
      form.removeControl('first_name');
      // @ts-ignore
      form.removeControl('last_name');
      // @ts-ignore
      form.removeControl('mobile_number');
      // @ts-ignore
      form.removeControl('national_code');
    } else {
      // @ts-ignore
      form.removeControl('company_name');
      // @ts-ignore
      form.removeControl('registration_number');
    }
    return form;
  };
  override form = this.initForm();

  override submitForm(payload: IProfileRequestPayload) {
    return this.service.updateProfile(payload);
  }
}
