import { AbstractFormDialog } from '@/shared/abstractClasses';
import { InputComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { Component, computed, inject, Input } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { IDeviceBrandRequest, IDeviceBrandResponse } from '../models';
import { DeviceBrandsService } from '../services/main.service';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';

@Component({
  selector: 'deviceBrand-form',
  templateUrl: './form.component.html',
  imports: [ReactiveFormsModule, SharedDialogComponent, InputComponent, FormFooterActionsComponent],
})
export class DeviceBrandFormComponent extends AbstractFormDialog<
  IDeviceBrandRequest,
  IDeviceBrandResponse
> {
  @Input() brandId?: string;
  private service = inject(DeviceBrandsService);

  form = this.fb.group({
    name: this.fb.control<string>(this.initialValues?.name || '', {
      nonNullable: true,
      validators: [Validators.required],
    }),
  });

  preparedTitle = computed(() => `${this.editMode ? 'ویرایش' : 'ایجاد'} برند دستگاه`);

  override ngOnChanges() {
    this.form.patchValue(this.initialValues ?? {});
    if (this.editMode && !this.brandId) {
      throw 'missing some arguments';
    }
  }

  override submitForm(payload: IDeviceBrandRequest) {
    if (this.editMode) {
      return this.service.update(this.brandId!, payload);
    }
    return this.service.create(payload);
  }
}
