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 { ConsumerAccountListComponent } from '../components/accounts/list.component';
import { ConsumerBusinessActivitiesComponent } from '../components/businessActivities/list.component';
import { ConsumerUserFormComponent } from '../components/form.component';
import { ConsumerStore } from '../store/consumer.store';

@Component({
  selector: 'consumer-user',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    KeyValueComponent,
    ConsumerUserFormComponent,
    ConsumerAccountListComponent,
    ConsumerBusinessActivitiesComponent,
  ],
})
export class ConsumerComponent {
  private readonly store = inject(ConsumerStore);
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);

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

  visibleLicenseForm = signal(false);

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

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

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

  setBreadcrumb() {
    this.breadcrumbService.setItems(this.store.breadcrumbItems());
  }

  openLicenseForm() {
    this.visibleLicenseForm.set(true);
  }
}
