import { MustMatch } from '@/core/validators';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import { SharedPasswordInputComponent, UsernameComponent } from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants/fields';
import { Component, computed, inject, Input } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { Divider } from 'primeng/divider';
import { IAdminConsumerRequest, IAdminConsumerResponse } from '../models';
import { ConsumersService } from '../services/main.service';

@Component({
  selector: 'superAdmin-consumer-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    UsernameComponent,
    FormFooterActionsComponent,
    Divider,
    SharedPasswordInputComponent,
  ],
})
export class ConsumerUserFormComponent extends AbstractFormDialog<
  IAdminConsumerRequest,
  IAdminConsumerResponse
> {
  @Input() consumerId?: string;
  private service = inject(ConsumersService);

  preparedTitle = computed(() => `${this.editMode ? 'ویرایش' : 'افزودن'} مشتری`);

  initForm = () => {
    const form = this.fb.group({
      // first_name: [this.initialValues?.first_name || '', [Validators.required]],
      // last_name: [this.initialValues?.last_name || '', [Validators.required]],
      // mobile_number: [
      //   this.initialValues?.mobile_number || '',
      //   [Validators.required, mobileValidator()],
      // ],
      username: [''],
      password: [''],
      confirmPassword: [''],
      // license: this.fb.group({
      //   starts_at: [this.initialValues?.license_info?.starts_at || ''],
      //   partner_id: [this.initialValues?.license_info?.partner?.id || ''],
      // }),
    });

    if (this.editMode) {
      // @ts-ignore
      form.removeControl('username');
      // @ts-ignore
      form.removeControl('password');
      // @ts-ignore
      form.removeControl('confirmPassword');
      form.removeValidators([MustMatch('password', 'confirmPassword')]);
    } else {
      // @ts-ignore
      form.addControl(
        'username',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: fieldControl.username('')[1],
        }),
      );
      // @ts-ignore
      form.addControl(
        'password',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: [Validators.required],
        }),
      );
      // @ts-ignore
      form.addControl(
        'confirmPassword',
        this.fb.control<string>('', {
          nonNullable: true,
          validators: [Validators.required],
        }),
      );
      form.addValidators([MustMatch('password', 'confirmPassword')]);
    }

    return form;
  };

  form = this.initForm();

  override ngOnChanges() {
    // this.form.patchValue(this.initialValues ?? {});
    // this.form.patchValue({
    //   ...(this.initialValues ?? {}),
    //   license: {
    //     ...(this.initialValues?.license_info ?? {}),
    //     partner_id: this.initialValues?.license_info?.partner?.id || '',
    //   },
    // });
    if (this.editMode && !this.consumerId) {
      throw 'missing some arguments';
    }

    this.form = this.initForm();
  }

  override submitForm(payload: IAdminConsumerRequest) {
    if (this.editMode) {
      return this.service.update(this.consumerId!, payload);
    }
    // @ts-ignore
    const { confirmPassword, ...rest } = payload;
    return this.service.create(rest);
  }
}
