import { DatePipe } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from '@angular/core';
import { ReceiptListItem } from '@isa/oms/data-access';
import { ClientRowImports, ItemRowDataImports } from '@isa/ui/item-rows';
import { formatName } from '@isa/utils/format-name';

@Component({
  selector: 'oms-feature-return-search-result-item',
  templateUrl: './return-search-result-item.component.html',
  styleUrls: ['./return-search-result-item.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ClientRowImports, ItemRowDataImports, DatePipe],
})
export class ReturnSearchResultItemComponent {
  item = input.required<ReceiptListItem>();

  name = computed(() => {
    const firstName = this.item()?.billing?.person?.firstName;
    const lastName = this.item()?.billing?.person?.lastName;
    const organisationName = this.item()?.billing?.organisation?.name;
    return formatName({ firstName, lastName, organisationName });
  });

  receiptDate = computed(() => {
    return this.item()?.printedDate;
  });

  receiptNumber = computed(() => {
    return this.item()?.receiptNumber;
  });

  orderNumber = computed(() => {
    return this.item()?.orderNumber;
  });

  email = computed(() => {
    return this.item()?.billing?.communicationDetails?.email;
  });

  address = computed(() => {
    const address =
      this.item()?.billing?.address ?? this.item()?.shipping?.address;
    return address ? [address.zipCode, address.city].join(' ') : '';
  });
}
