import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ParcelamentoListItem } from '../../../../services/parcelamentos.service';

export type ParcelamentoSegment = 'todos' | 'ativos' | 'futuros' | 'finalizados';

export type ParcelamentoViewItem = ParcelamentoListItem & {
  status: 'ativos' | 'futuros' | 'finalizados';
  statusLabel: string;
  progressLabel: string;
  valorParcela?: number | null;
  valorCheioNumber?: number | null;
  descontoNumber?: number | null;
  valorComDescontoNumber?: number | null;
};

@Component({
  selector: 'app-parcelamentos-table',
  standalone: true,
  imports: [CommonModule, FormsModule],
  templateUrl: './parcelamentos-table.html',
  styleUrls: ['./parcelamentos-table.scss'],
})
export class ParcelamentosTableComponent {
  @Input() items: ParcelamentoViewItem[] = [];
  @Input() loading = false;
  @Input() errorMessage = '';
  @Input() isSysAdmin = false;

  @Input() segment: ParcelamentoSegment = 'todos';
  @Input() segmentCounts: Record<ParcelamentoSegment, number> = {
    todos: 0,
    ativos: 0,
    futuros: 0,
    finalizados: 0,
  };

  @Input() page = 1;
  @Input() pageNumbers: number[] = [];
  @Input() pageStart = 0;
  @Input() pageEnd = 0;
  @Input() total = 0;
  @Input() pageSize = 10;
  @Input() pageSizeOptions: number[] = [];

  @Output() segmentChange = new EventEmitter<ParcelamentoSegment>();
  @Output() detail = new EventEmitter<ParcelamentoViewItem>();
  @Output() edit = new EventEmitter<ParcelamentoViewItem>();
  @Output() remove = new EventEmitter<ParcelamentoViewItem>();
  @Output() pageChange = new EventEmitter<number>();
  @Output() pageSizeChange = new EventEmitter<number>();

  readonly segments: Array<{ key: ParcelamentoSegment; label: string }> = [
    { key: 'todos', label: 'Lista geral' },
    { key: 'ativos', label: 'Ativos' },
    { key: 'futuros', label: 'Futuros' },
    { key: 'finalizados', label: 'Finalizados' },
  ];

  skeletonRows = Array.from({ length: 6 });

  trackById(_: number, item: ParcelamentoViewItem): string {
    return item.id;
  }
}
