import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { ButtonComponent } from '@isa/ui/buttons';
import { injectTabId, TabService } from '@isa/core/tabs';
import { Router } from '@angular/router';
import { getRouteToCustomer } from '../../helpers';

@Component({
  selector: 'reward-start-card',
  templateUrl: './reward-start-card.component.html',
  styleUrl: './reward-start-card.component.css',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ButtonComponent],
})
export class RewardStartCardComponent {
  readonly #tabService = inject(TabService);
  readonly #router = inject(Router);

  tabId = injectTabId();

  /**
   * Called when "Kund*in auswählen" button is clicked.
   * Preserves the current URL as returnUrl before navigating to customer search.
   */
  onSelectCustomer() {
    const customerRoute = getRouteToCustomer(this.tabId());
    const tabId = this.#tabService.activatedTabId();

    // Preserve context: Store current reward page URL to return to after customer selection
    if (tabId) {
      this.#tabService.patchTabMetadata(tabId, {
        'select-customer': { returnUrl: this.#router.url },
      });
    }

    // Navigate to customer search
    this.#router.navigate(customerRoute.path, {
      queryParams: customerRoute.queryParams,
    });
  }
}
