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 { IGoodCategoriesResponse, IGoodCategoryRequest } from '../../models';
import { GuildGoodCategoriesService } from '../../services/goodCategories.service';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';

@Component({
  selector: 'admin-guild-good-categories-form',
  templateUrl: './form.component.html',
  imports: [ReactiveFormsModule, SharedDialogComponent, InputComponent, FormFooterActionsComponent],
})
export class GuildGoodCategoryFormComponent extends AbstractFormDialog<
  IGoodCategoryRequest,
  IGoodCategoriesResponse
> {
  @Input() guildId!: string;
  @Input() categoryId?: string;

  private service = inject(GuildGoodCategoriesService);

  form = this.fb.group({
    name: [this.initialValues?.name || '', [Validators.required]],
  });

  override submitForm(payload: IGoodCategoryRequest) {
    if (this.editMode) {
      return this.service.update(this.guildId!, this.categoryId!, payload);
    }
    return this.service.create(this.guildId!, payload);
  }
}
