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 { ConsumerComplexFormComponent } from '../../components/complexes/form.component';
import { ConsumerPosesComponent } from '../../components/poses/list.component';
import { BusinessActivityStore } from '../../store/businessActivity.store';
import { ConsumerComplexStore } from '../../store/complex.store';

@Component({
  selector: 'consumer-complex',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    KeyValueComponent,
    ConsumerComplexFormComponent,
    ConsumerPosesComponent,
  ],
})
export class ConsumerComplexComponent {
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);
  private readonly store = inject(ConsumerComplexStore);
  private readonly businessStore = inject(BusinessActivityStore);

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

  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());
  readonly business = computed(() => this.businessStore.entity());

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

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

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