import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
} from '@angular/core';
import { coerceNumberProperty, NumberInput } from '@angular/cdk/coercion';
import { ButtonComponent, IconButtonComponent } from '@isa/ui/buttons';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { isaActionChevronLeft, isaLoading } from '@isa/icons';
import { RemissionReturnReceiptDetailsCardComponent } from './remission-return-receipt-details-card.component';
import { RemissionReturnReceiptDetailsItemComponent } from './remission-return-receipt-details-item.component';
import { Location } from '@angular/common';
import { createReturnResource } from './resources/return.resource';
import {
  getPackageNumbersFromReturn,
  getReceiptItemsFromReturn,
  getReceiptNumberFromReturn,
} from '@isa/remission/data-access';
import { EmptyStateComponent } from '@isa/ui/empty-state';
import { EMPTY_WBS_DESCRIPTION, EMPTY_WBS_TITLE } from './constants';
import {
  RemissionReturnReceiptActionsComponent,
  RemissionReturnReceiptCompleteComponent,
} from '@isa/remission/shared/return-receipt-actions';

@Component({
  selector: 'remi-remission-return-receipt-details',
  templateUrl: './remission-return-receipt-details.component.html',
  styleUrls: ['./remission-return-receipt-details.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [
    ButtonComponent,
    IconButtonComponent,
    NgIcon,
    RemissionReturnReceiptDetailsCardComponent,
    RemissionReturnReceiptDetailsItemComponent,
    EmptyStateComponent,
    RemissionReturnReceiptActionsComponent,
    RemissionReturnReceiptCompleteComponent,
  ],
  providers: [provideIcons({ isaActionChevronLeft, isaLoading })],
})
export class RemissionReturnReceiptDetailsComponent {
  /** Title for the empty state when no remission return receipt is available */
  emptyWbsTitle = EMPTY_WBS_TITLE;

  /** Description for the empty state when no remission return receipt is available */
  emptyWbsDescription = EMPTY_WBS_DESCRIPTION;

  /** Angular Location service for navigation */
  location = inject(Location);

  /**
   * Required input for the return ID.
   * Automatically coerced to a number from string input.
   * @input
   * @required
   */
  returnId = input.required<number, NumberInput>({
    transform: coerceNumberProperty,
  });

  /**
   * Required input for the receipt ID.
   * Automatically coerced to a number from string input.
   * @input
   * @required
   */
  receiptId = input.required<number, NumberInput>({
    transform: coerceNumberProperty,
  });

  /**
   * Computed signal that retrieves the current remission return receipt.
   * This is used to display detailed information about the return receipt.
   * @returns {Return} The remission return receipt data
   */
  returnResource = createReturnResource(() => ({
    returnId: this.returnId(),
    eagerLoading: 3,
  }));

  returnLoading = computed(() => this.returnResource.isLoading());

  returnData = computed(() => this.returnResource.value());

  /**
   * 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.returnData();
    return getReceiptNumberFromReturn(returnData!);
  });

  receiptItems = computed(() => {
    const returnData = this.returnData();
    return getReceiptItemsFromReturn(returnData!);
  });

  canRemoveItems = computed(() => {
    const returnData = this.returnData();
    return !!returnData && !returnData.completed;
  });

  hasAssignedPackage = computed(() => {
    const returnData = this.returnData();
    return getPackageNumbersFromReturn(returnData!) !== '';
  });
}
