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 { EnumSelectComponent } from '@/shared/apiEnum/select.component';
import { CatalogGuildGoodCategoriesSelectComponent } from '@/shared/catalog/guildGoodCategories';
import {
  IConsumerBusinessActivityGoodRequest,
  IConsumerBusinessActivityGoodResponse,
} from '../../models/goods_io';
import { BusinessActivityGoodsService } from '../../services/goods.service';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';

@Component({
  selector: 'consumer-businessActivity-good-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    InputComponent,
    FormFooterActionsComponent,
    EnumSelectComponent,
    CatalogGuildGoodCategoriesSelectComponent,
  ],
})
export class ConsumerBusinessActivityGoodFormComponent extends AbstractFormDialog<
  IConsumerBusinessActivityGoodRequest,
  IConsumerBusinessActivityGoodResponse
> {
  @Input({ required: true }) businessId!: string;
  @Input({ required: true }) guildId!: string;
  @Input() categoryId?: string;

  private service = inject(BusinessActivityGoodsService);

  form = this.fb.group({
    name: [this.initialValues?.name || '', [Validators.required]],
    sku: [this.initialValues?.sku || '', [Validators.required]],
    unit_type: [this.initialValues?.unit_type || '', [Validators.required]],
    pricing_model: [this.initialValues?.pricing_model || '', [Validators.required]],
    category_id: [this.initialValues?.category.id || '', [Validators.required]],
    description: [this.initialValues?.description || ''],
  });

  get preparedTitle() {
    return `${this.editMode ? 'ویرایش' : 'ایجاد'} کالا`;
  }

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