import { BreadcrumbService } from '@/core/services';
import { PartnerLicenseInfoTemplateComponent } from '@/domains/partner/modules/dashboard/components/licenseInfo/license-info-template.component';
import { AppCardComponent, KeyValueComponent } from '@/shared/components';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Button } from 'primeng/button';
import { ConsumerAccountListComponent } from '../components/accounts/list.component';
import { PartnerChargeLicenseFormDialogComponent } from '../components/charge-license-form-dialog.component';
import { AdminPartnerChargeAccountFormDialogComponent } from '../components/chargeAccount/charge-account-form-dialog.component';
import { AdminPartnerChargeAccountListComponent } from '../components/chargeAccount/list.component';
import { AdminPartnerChargeLicenseTransactionListComponent } from '../components/chargeLicenseTransactions/list.component';
import { GuildFormComponent } from '../components/form.component';
import { AdminPartnerLicensesComponent } from '../components/licenses/list.component';
import { superAdminPartnersNamedRoutes } from '../constants';
import { PartnerStore } from '../store/partner.store';

@Component({
  selector: 'app-user',
  templateUrl: './single.component.html',
  imports: [
    AppCardComponent,
    KeyValueComponent,
    GuildFormComponent,
    ConsumerAccountListComponent,
    Button,
    AdminPartnerChargeAccountListComponent,
    AdminPartnerChargeAccountFormDialogComponent,
    PartnerChargeLicenseFormDialogComponent,
    AdminPartnerLicensesComponent,
    AdminPartnerChargeLicenseTransactionListComponent,
    PartnerLicenseInfoTemplateComponent,
  ],
})
export class PartnerComponent {
  private readonly store = inject(PartnerStore);
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);

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

  readonly loading = computed(() => this.store.loading());
  readonly partner = computed(() => this.store.entity());
  visibleChargeFormDialog = signal(false);
  visibleChargeAccountFormDialog = signal(false);

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

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

  setBreadcrumb() {
    this.breadcrumbService.setItems([
      {
        ...superAdminPartnersNamedRoutes.partners.meta,
        routerLink: superAdminPartnersNamedRoutes.partners.meta.pagePath!(),
      },
      {
        title: this.partner()?.name,
      },
    ]);
  }

  openChargeDialog() {
    this.visibleChargeFormDialog.set(true);
  }

  openChargeAccountDialog() {
    this.visibleChargeAccountFormDialog.set(true);
  }
}
