import { Component } from '@angular/core';
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { ICellRendererParams } from 'ag-grid-community';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { TeamActivity } from '../../models/transaction.model';

export interface ReverseActionCellRendererParams extends ICellRendererParams<TeamActivity> {
  canReverse: (activity: TeamActivity) => boolean;
  onReverse: (activity: TeamActivity) => void;
}

@Component({
  selector: 'app-reverse-action-cell-renderer',
  imports: [MatButtonModule, MatIconModule],
  template: `
    @if (activity && canReverseActivity) {
      <button
        mat-icon-button
        data-testid="reverse-booking"
        aria-label="Buchung stornieren"
        (click)="onReverse()"
      >
        <mat-icon>undo</mat-icon>
      </button>
    }
  `,
})
export class ReverseActionCellRenderer implements ICellRendererAngularComp {
  protected activity?: TeamActivity;
  protected canReverseActivity = false;
  private params?: ReverseActionCellRendererParams;

  agInit(params: ReverseActionCellRendererParams): void {
    this.params = params;
    this.activity = params.data;
    this.canReverseActivity = this.activity ? params.canReverse(this.activity) : false;
  }

  refresh(params: ReverseActionCellRendererParams): boolean {
    this.agInit(params);
    return true;
  }

  protected onReverse(): void {
    if (this.activity) this.params?.onReverse(this.activity);
  }
}
