import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  effect,
} from '@angular/core';
import { ActivatedRoute, RouterLink } from '@angular/router';
import {
  getReceiptItemQuantityFromReturn,
  getReceiptNumberFromReturn,
  RemissionStore,
} from '@isa/remission/data-access';
import { ButtonComponent } from '@isa/ui/buttons';

@Component({
  selector: 'remi-feature-remission-return-card',
  templateUrl: './remission-return-card.component.html',
  styleUrl: './remission-return-card.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RouterLink, ButtonComponent],
})
export class RemissionReturnCardComponent {
  route = inject(ActivatedRoute);
  #remissionStore = inject(RemissionStore);

  returnId = computed(() => this.#remissionStore.returnId());
  receiptId = computed(() => this.#remissionStore.receiptId());

  receiptItemsCount = computed(() => {
    const returnData = this.#remissionStore.returnData();
    return getReceiptItemQuantityFromReturn(returnData!);
  });

  receiptNumber = computed(() => {
    const returnData = this.#remissionStore.returnData();
    return getReceiptNumberFromReturn(returnData!);
  });

  constructor() {
    effect(() => {
      this.returnId();
      this.receiptId();
      this.#remissionStore.reloadReturn();
    });
  }
}
