import { BreadcrumbService } from '@/core/services';
import { AppCardComponent, KeyValueComponent } from '@/shared/components';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ConsumerBusinessActivityFormComponent } from '../components/form-dialog.component';
import { ConsumerCustomerSaleInvoiceListComponent } from '../components/saleInvoices/list.component';
import { ConsumerCustomerStore } from '../store/customer.store';

@Component({
  selector: 'consumer-customer',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    ConsumerBusinessActivityFormComponent,
    KeyValueComponent,
    ConsumerCustomerSaleInvoiceListComponent,
  ],
})
export class ConsumerCustomerComponent {
  private readonly store = inject(ConsumerCustomerStore);
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);

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

  readonly loading = computed(() => this.store.loading());
  readonly customer = computed(() => this.store.entity());
  readonly customerBreadcrumb = computed(() => this.store.breadcrumbItems());

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

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

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