import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
} from '@angular/core';
import { isaActionChevronDown, isaNavigationKunden } from '@isa/icons';
import { InfoButtonComponent } from '@isa/ui/buttons';
import { NgIconComponent, provideIcons } from '@ng-icons/core';
import { formatName } from '@isa/utils/format-name';
import { UiMenu } from '@isa/ui/menu';
import { RouterLink } from '@angular/router';
import { CdkMenuTrigger } from '@angular/cdk/menu';
import { ReturnDetailsStore } from '@isa/oms/data-access';

@Component({
  selector: 'oms-feature-return-details-header',
  templateUrl: './return-details-header.component.html',
  styleUrls: ['./return-details-header.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [
    InfoButtonComponent,
    NgIconComponent,
    UiMenu,
    RouterLink,
    CdkMenuTrigger,
  ],
  providers: [provideIcons({ isaNavigationKunden, isaActionChevronDown })],
})
export class ReturnDetailsHeaderComponent {
  #store = inject(ReturnDetailsStore);

  receiptId = input.required<number>();

  receipt = this.#store.getReceipt(this.receiptId);

  buyer = computed(() => this.receipt().buyer);

  referenceId = computed(() => {
    const buyer = this.buyer();
    return buyer?.reference?.id;
  });

  name = computed(() => {
    const buyer = this.buyer();
    if (!buyer) {
      return '';
    }

    const firstName = this.buyer()?.firstName;
    const lastName = this.buyer()?.lastName;
    const organisationName = this.buyer()?.organisation?.name;
    return formatName({ firstName, lastName, organisationName });
  });

  now() {
    return Date.now();
  }
}
