import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
} from '@angular/core';
import {
  SelectedCustomerResource,
  getCustomerName,
  SelectedCustomerShippingAddressResource,
  SelectedCustomerPayerAddressResource,
} from '@isa/crm/data-access';
import { isaActionEdit } from '@isa/icons';
import { IconButtonComponent } from '@isa/ui/buttons';
import { provideIcons } from '@ng-icons/core';
import { AddressComponent } from '@isa/shared/address';
import { injectTabId, TabService } from '@isa/core/tabs';
import { Router } from '@angular/router';

@Component({
  selector: 'checkout-billing-and-shipping-address-card',
  templateUrl: './billing-and-shipping-address-card.component.html',
  styleUrls: ['./billing-and-shipping-address-card.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [IconButtonComponent, AddressComponent],
  providers: [provideIcons({ isaActionEdit })],
})
export class BillingAndShippingAddressCardComponent {
  #tabService = inject(TabService);
  #router = inject(Router);
  #shippingAddressResource = inject(SelectedCustomerShippingAddressResource);
  #payerAddressResource = inject(SelectedCustomerPayerAddressResource);

  tabId = injectTabId();
  #customerResource = inject(SelectedCustomerResource).resource;

  isLoading = computed(() => {
    return (
      this.#customerResource.isLoading() ||
      this.#shippingAddressResource.resource.isLoading() ||
      this.#payerAddressResource.resource.isLoading()
    );
  });

  customer = computed(() => {
    return this.#customerResource.value();
  });

  navigateToCustomer() {
    const customerId = this.customer()?.id;
    const tabId = this.tabId();
    if (!customerId || !tabId) return;

    const returnUrl = `/${tabId}/reward/cart`;

    // Preserve context across intermediate navigations (scoped to active tab)
    this.#tabService.patchTabMetadata(tabId, {
      'select-customer': { returnUrl },
    });

    this.#router.navigate(['/', 'kunde', tabId, 'customer', 'search', customerId]);
  }

  payer = computed(() => {
    // Prefer selected payer from metadata over customer as payer
    const selectedPayer = this.#payerAddressResource.resource.value();
    if (selectedPayer) {
      return selectedPayer;
    }
    // Fallback to customer as payer
    return this.customer();
  });

  payerName = computed(() => {
    const payer = this.payer();
    return getCustomerName(payer);
  });

  payerAddress = computed(() => {
    const payer = this.payer();
    if (!payer) return undefined;
    return payer.address;
  });

  shippingAddress = computed(() => {
    // Prefer selected shipping address from metadata over customer default
    const selectedAddress = this.#shippingAddressResource.resource.value();
    if (selectedAddress) {
      return selectedAddress;
    }
    // Fallback to customer
    return this.customer();
  });

  shippingName = computed(() => {
    const shipping = this.shippingAddress();
    return getCustomerName(shipping);
  });

  shippingAddressAddress = computed(() => {
    const shipping = this.shippingAddress();
    if (!shipping) return undefined;
    return shipping.address;
  });
}
