import { mobileValidator } from '@/core/validators';
// import { CatalogRolesComponent } from '@/shared/catalog/roles';
import { AbstractFormDialog } from '@/shared/abstractClasses';
import { InputComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { IUserRequest, IUserResponse } from '../models';
import { UsersService } from '../services/main.service';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';

@Component({
  selector: 'superAdmin-user-form',
  templateUrl: './form.component.html',
  imports: [ReactiveFormsModule, SharedDialogComponent, InputComponent, FormFooterActionsComponent],
})
export class UserAccountFormComponent extends AbstractFormDialog<IUserRequest, IUserResponse> {
  private readonly service = inject(UsersService);

  @Input() userId!: string;
  @Input() accountId!: string;

  initForm = () => {
    if (this.editMode) {
      return 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()],
          ],
          // @ts-ignore
          // role: [this.initialValues?.role || '', [Validators.required]],

          // password: ['', [password()]],
          // confirmPassword: [''],
        },
        // {
        //   validators: [MustMatch('password', 'confirmPassword')],
        // },
      );
    }
    return 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()],
        ],
        // @ts-ignore
        // role: [this.initialValues?.role || '', [Validators.required]],
        // password: ['', [Validators.required, password()]],
        // confirmPassword: ['', [Validators.required]],
      },
      // {
      //   validators: [MustMatch('password', 'confirmPassword')],
      // },
    );
  };

  form = this.initForm();

  override ngOnChanges(): void {
    this.form = this.initForm();
  }

  submitForm() {
    const formValue = this.form.value as IUserRequest;
    if (this.editMode) {
      return this.service.update(this.userId, formValue);
    }
    return this.service.create(formValue);
  }
}
