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

@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 #navigationState = inject(NavigationStateService);
  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.
   */
  async onSelectCustomer() {
    const customerRoute = getRouteToCustomer(this.tabId());

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

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