import { BreadcrumbService } from '@/core/services';
import { AppCardComponent, KeyValueComponent } from '@/shared/components';
import pageParamsUtils from '@/utils/page-params.utils';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ConsumerComplexFormDialogComponent } from '../../components/complexes/form-dialog.component';
import { ConsumerPosesComponent } from '../../components/poses/list.component';
import { BusinessActivityStore } from '../../store/businessActivity.store';
import { ComplexStore } from '../../store/complex.store';
import { ConsumerStore } from '../../store/consumer.store';

@Component({
  selector: 'partner-user-complex',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    KeyValueComponent,
    ConsumerComplexFormDialogComponent,
    ConsumerPosesComponent,
  ],
})
export class PartnerUserComplexComponent {
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);
  private readonly store = inject(ComplexStore);
  private readonly consumerStore = inject(ConsumerStore);
  private readonly businessStore = inject(BusinessActivityStore);

  pageParams = computed(() => pageParamsUtils(this.route));

  readonly consumerId = signal<string>(this.pageParams()['consumerId']!);
  readonly businessId = signal<string>(this.pageParams()['businessActivityId']!);
  readonly complexId = signal<string>(this.pageParams()['complexId']!);

  editMode = signal<boolean>(false);

  readonly loading = computed(() => this.store.loading());
  readonly complex = computed(() => this.store.entity());

  constructor() {
    effect(() => {
      if (this.complex()?.id) {
        this.setBreadcrumb();
      }
    });
  }

  getData() {
    this.store.getData(this.consumerId(), this.businessId(), this.complexId());
  }

  setBreadcrumb() {
    this.breadcrumbService.setItems([
      ...this.consumerStore.breadcrumbItems(),
      ...this.businessStore.breadcrumbItems(),
      ...this.store.breadcrumbItems(),
    ]);
  }
}
