import { BreadcrumbService } from '@/core/services';
import { AppCardComponent, KeyValueComponent } from '@/shared/components';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { ButtonDirective } from 'primeng/button';
import { Divider } from 'primeng/divider';
import { ConsumerComplexListComponent } from '../components/complexes/list.component';
import { ConsumerBusinessActivityFormComponent } from '../components/form.component';
import { consumerBusinessActivityGoodsNamedRoutes } from '../constants/routes/goods';
import { BusinessActivityStore } from '../store/businessActivity.store';

@Component({
  selector: 'business-businessActivity',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    KeyValueComponent,
    ConsumerBusinessActivityFormComponent,
    ConsumerComplexListComponent,
    Divider,
    ButtonDirective,
    RouterLink,
  ],
})
export class ConsumerBusinessActivityComponent {
  constructor() {
    effect(() => {
      if (this.business()?.id) {
        this.setBreadcrumb();
      }
    });
  }

  private readonly store = inject(BusinessActivityStore);
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);

  readonly businessId = signal<string>(this.route.snapshot.paramMap.get('businessActivityId')!);
  editMode = signal<boolean>(false);

  readonly loading = computed(() => this.store.loading());
  readonly business = computed(() => this.store.entity());
  readonly businessBreadcrumb = computed(() => this.store.breadcrumbItems());

  readonly goodsPageRoute = computed(() =>
    this.business()
      ? consumerBusinessActivityGoodsNamedRoutes.goods.meta.pagePath!(this.business()!.id)
      : '',
  );

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

  setBreadcrumb() {
    this.breadcrumbService.setItems(this.businessBreadcrumb());
  }
}
