import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
  linkedSignal,
  output,
} from '@angular/core';
import {
  Return,
  ReceiptCompleteStatusValue,
  ReceiptCompleteStatus,
  getReceiptStatusFromReturn,
  getReceiptItemQuantityFromReturn,
  getReceiptNumberFromReturn,
} from '@isa/remission/data-access';
import { RemissionReturnReceiptActionsComponent } from '@isa/remission/shared/return-receipt-actions';

/**
 * Component that displays a list item for a remission return receipt.
 * Shows the receipt number, item quantity, and status of the return.
 *
 * @component
 * @selector remi-return-receipt-list-item
 * @standalone
 *
 * @example
 * <remi-return-receipt-list-item [remissionReturn]="returnData"></remi-return-receipt-list-item>
 */
@Component({
  selector: 'remi-return-receipt-list-item',
  templateUrl: './return-receipt-list-item.component.html',
  styleUrls: ['./return-receipt-list-item.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [RemissionReturnReceiptActionsComponent],
  host: {
    class: 'remi-return-receipt-list-item',
  },
})
export class ReturnReceiptListItemComponent {
  /** Constant for receipt completion status */
  ReceiptCompleteStatus = ReceiptCompleteStatus;

  /**
   * Input for the remission return data.
   * @input
   */
  remissionReturn = input.required<Return>();

  /**
   * Output event that emits when the list needs to be reloaded.
   * @output
   */
  reloadList = output<void>();

  /**
   * Computed signal that retrieves the receipt number from the return data.
   * Uses the helper function to get the receipt number.
   * @returns {string} The receipt number from the return
   */
  receiptNumber = computed(() => {
    const returnData = this.remissionReturn();
    return getReceiptNumberFromReturn(returnData);
  });

  /**
   * Computed signal that calculates the total item quantity from all receipts in the return.
   * Uses the helper function to get the quantity.
   * @returns {number} The total item quantity from all receipts
   */
  itemQuantity = computed(() => {
    const returnData = this.remissionReturn();
    return getReceiptItemQuantityFromReturn(returnData);
  });

  /**
   * Linked signal that determines the completion status of the return.
   * Uses the helper function to get the status based on the return data.
   * @returns {ReceiptCompleteStatusValue} The completion status of the return
   */
  status = linkedSignal<ReceiptCompleteStatusValue>(() => {
    const returnData = this.remissionReturn();
    return getReceiptStatusFromReturn(returnData);
  });
}
