import { AbstractFormDialog } from '@/shared/abstractClasses';
import {
  EconomicCodeComponent,
  FiscalIdComponent,
  NameComponent,
  PartnerTokenComponent,
} from '@/shared/components';
import { SharedDialogComponent } from '@/shared/components/dialog/dialog.component';
import { InvoiceNumberSequenceComponent } from '@/shared/components/fields/invoice_number_sequence.component';
import { FormFooterActionsComponent } from '@/shared/components/formFooterActions/form-footer-actions.component';
import { fieldControl } from '@/shared/constants/fields';
import { Component, inject, Input } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { IBusinessActivityRequest, IBusinessActivityResponse } from '../models';
import { BusinessActivitiesService } from '../services/main.service';

@Component({
  selector: 'consumer-businessActivity-form',
  templateUrl: './form.component.html',
  imports: [
    ReactiveFormsModule,
    SharedDialogComponent,
    FormFooterActionsComponent,
    NameComponent,
    EconomicCodeComponent,
    PartnerTokenComponent,
    InvoiceNumberSequenceComponent,
    FiscalIdComponent,
  ],
})
export class ConsumerBusinessActivityFormComponent extends AbstractFormDialog<
  IBusinessActivityRequest,
  IBusinessActivityResponse
> {
  @Input({ required: true }) businessId!: string;
  private service = inject(BusinessActivitiesService);

  form = this.fb.group({
    name: fieldControl.name(this.initialValues?.name || ''),
    economic_code: fieldControl.economic_code(this.initialValues?.economic_code || ''),
    fiscal_id: fieldControl.fiscal_id(this.initialValues?.fiscal_id || ''),
    partner_token: fieldControl.partner_token(this.initialValues?.partner_token || ''),
    invoice_number_sequence: fieldControl.invoice_number_sequence(
      this.initialValues?.invoice_number_sequence || 1,
    ),
  });

  override submitForm(payload: IBusinessActivityRequest) {
    return this.service.update(this.businessId, {
      ...payload,
      economic_code: payload.economic_code.toString(),
    });
  }
}
