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 { ConsumerPosFormDialogComponent } from '../../components/poses/form-dialog.component';
import { BusinessActivityStore } from '../../store/businessActivity.store';
import { ComplexStore } from '../../store/complex.store';
import { ConsumerStore } from '../../store/consumer.store';
import { PosStore } from '../../store/pos.store';

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

  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']!);
  readonly posId = signal<string>(this.pageParams()['posId']!);
  editMode = signal<boolean>(false);

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

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

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

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