import { AbstractFormDialog } from '@/shared/abstractClasses';
import { InputComponent, SkuComponent } from '@/shared/components';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { Component, inject, Input, signal } from '@angular/core';
import { ReactiveFormsModule, Validators } from '@angular/forms';

import { Maybe } from '@/core';
import { EnumSelectComponent } from '@/shared/apiEnum/select.component';
import { CatalogMeasureUnitsSelectComponent } from '@/shared/catalog/measureUnits';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { onSelectFileArgs } from '@/shared/components/uploadFile/model';
import { SharedUploadFileComponent } from '@/shared/components/uploadFile/upload-file.component';
import { buildFormData } from '@/utils';
import { IGuildGoodRequest, IGuildGoodsResponse } from '../../models';
import { GuildGoodsService } from '../../services/goods.service';
import { GuildCategoriesSelectComponent } from '../categories/select.component';

@Component({
  selector: 'admin-guild-good-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    InputComponent,
    FormFooterActionsComponent,
    EnumSelectComponent,
    GuildCategoriesSelectComponent,
    SharedUploadFileComponent,
    SkuComponent,
    CatalogMeasureUnitsSelectComponent,
  ],
})
export class GuildGoodFormComponent extends AbstractFormDialog<
  IGuildGoodRequest,
  IGuildGoodsResponse
> {
  @Input({ required: true }) guildId!: string;
  @Input() categoryId?: string;

  private service = inject(GuildGoodsService);

  goodImage = signal<Maybe<File>>(null);

  form = this.fb.group({
    name: [this.initialValues?.name || '', [Validators.required]],
    sku_id: [this.initialValues?.sku.id || '', [Validators.required]],
    measure_unit_id: [this.initialValues?.measure_unit.id || '', [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 ? 'ویرایش' : 'افزودن'} کالا`;
  }

  changeFile(payload: onSelectFileArgs) {
    this.goodImage.set(payload.file);
  }

  override ngOnChanges(): void {
    this.form.patchValue(this.initialValues || {});
    this.form.controls.category_id.setValue(this.initialValues?.category.id || '');
    this.form.controls.measure_unit_id.setValue(this.initialValues?.measure_unit.id || '');
    this.form.controls.sku_id.setValue(this.initialValues?.sku.id || '');
  }

  override submitForm(payload: IGuildGoodRequest) {
    const formData = buildFormData(this.form, { image: this.goodImage() });
    if (this.editMode) {
      return this.service.update(this.guildId, this.categoryId!, formData);
    }
    return this.service.create(this.guildId, formData);
  }
}
