/**
 * Modèles pour les rapports d'activités
 */

export interface ReportFilters {
  date_from: string;
  date_to: string;
  account_id?: number;
}

export interface ReportSummary {
  total: number;
  matched: number;
  unmatched: number;
  matched_percentage: number;
  total_amount?: number;
  matched_amount?: number;
}

export interface Alert {
  severity: 'info' | 'warning' | 'critical';
  message: string;
  action_required: boolean;
  action_hint?: string;
  count?: number;
  total_gap?: number;
}

export interface ChartData {
  match_rate?: {
    matched: number;
    unmatched: number;
  };
  by_account?: AccountBreakdown[];
  daily_trend?: TrendPoint[];
}

export interface AccountBreakdown {
  account_id: number;
  account_name: string;
  office_id: string;
  total: number;
  matched: number;
  unmatched: number;
  match_rate: number;
}

export interface TrendPoint {
  date: string;
  value: number;
}

export interface ReportDetail {
  summary: ReportSummary;
  details: any;
  charts: ChartData;
  alerts: Alert[];
  export_ready?: boolean;
  generated_at: string;
}

export interface BanqueMouvementsReport extends ReportDetail {
  details: {
    unmatched_banques: UnmatchedBanque[];
    by_account: AccountBreakdown[];
  };
}

export interface UnmatchedBanque {
  id: number;
  uuid: string;
  name: string;
  amount: number;
  account_name: string;
  office_id: string;
  receive_date: string;
  status: string;
}

export interface FinancialRequestsReport extends ReportDetail {
  details: {
    unmatched_requests: UnmatchedFinancialRequest[];
    amount_discrepancies: AmountDiscrepancy[];
    by_category: CategoryBreakdown[];
  };
}

export interface UnmatchedFinancialRequest {
  id: number;
  uuid: string;
  name: string;
  category: string;
  amount: number;
  status: string;
  account_name: string;
}

export interface AmountDiscrepancy {
  financial_request_id?: number;
  creditlimit_id?: number;
  soldesbmp_id?: number;
  name?: string;
  amount1: number;
  amount2: number;
  gap: number;
  gap_percentage: number;
}

export interface CategoryBreakdown {
  category: string;
  total: number;
  matched: number;
  unmatched: number;
}

export interface CreditLimitsReport extends ReportDetail {
  details: {
    unlinked_creditlimits: UnlinkedCreditLimit[];
    amount_discrepancies: AmountDiscrepancy[];
    by_action: ActionBreakdown[];
  };
}

export interface UnlinkedCreditLimit {
  id: number;
  name: string;
  action: string;
  total_operation: number;
  recordlocator: string;
  document: string;
  account_name: string;
}

export interface ActionBreakdown {
  action: string;
  total: number;
  linked: number;
  unlinked: number;
}

export interface SoldesBmpReport extends ReportDetail {
  details: {
    unlinked_soldesbmp: UnlinkedSoldeBmp[];
    amount_discrepancies: AmountDiscrepancy[];
    by_account: AccountBreakdown[];
  };
}

export interface UnlinkedSoldeBmp {
  id: number;
  name: string;
  bmp_amount: number;
  balance_date: string;
  account_name: string;
}

export interface BmpEtatsdeventesReport extends ReportDetail {
  details: {
    unlinked_bmps: UnlinkedBmp[];
    by_account: AccountBreakdown[];
  };
}

export interface UnlinkedBmp {
  id: number;
  name: string;
  date: string;
  account_name: string;
  office_id: string;
}

export interface BspDetailEtatsdeventesReport extends ReportDetail {
  details: {
    unlinked_bspdetails: UnlinkedBspDetail[];
    by_trnc: TrncBreakdown[];
  };
}

export interface UnlinkedBspDetail {
  id: number;
  name: string;
  trnc: string;
  date: string;
}

export interface TrncBreakdown {
  trnc: string;
  total: number;
  linked: number;
  unlinked: number;
}

// Rapports individuels retournés par le dashboard
export interface BanqueMouvementsCard {
  report_type: 'banques_mouvements';
  total_depots: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

export interface FinancialRequestsCard {
  report_type: 'financial_requests_mouvements';
  total_financial_requests: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

export interface EtatsdeventesMouvementsCard {
  report_type: 'etatsdeventes_mouvements';
  total_etatsdeventes: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

export interface SoldesBmpCard {
  report_type: 'soldesbmp_soldes';
  total_soldesbmp: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

export interface BmpEtatsdeventesCard {
  report_type: 'bmps_etatsdeventes';
  total_bmps: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

export interface BspDetailEtatsdeventesCard {
  report_type: 'bspdetails_etatsdeventes';
  total_bspdetails: number;
  matched: number;
  unmatched: number;
  link_percentage: number;
  matched_percentage: number;
  health_status: 'good' | 'warning' | 'critical';
}

// Type union pour tous les rapports
export type ReportCardData =
  | BanqueMouvementsCard
  | FinancialRequestsCard
  | EtatsdeventesMouvementsCard
  | SoldesBmpCard
  | BmpEtatsdeventesCard
  | BspDetailEtatsdeventesCard;

// Alerte simplifiée pour le dashboard (selon doc ligne 503-509)
export interface DashboardAlert {
  report: string;
  status: 'info' | 'warning' | 'critical';
  percentage: number;
}

export interface RefreshLinksResponse {
  success: boolean;
  message?: string;
  report_type: string;
  links_created?: number;
  links_updated?: number;
}

export interface DashboardResponse {
  reports: {
    banques_mouvements: BanqueMouvementsCard;
    financial_requests_mouvements: FinancialRequestsCard;
    creditlimits: EtatsdeventesMouvementsCard;
    soldesbmp_soldes: SoldesBmpCard;
    bmps_etatsdeventes: BmpEtatsdeventesCard;
    bspdetails_etatsdeventes: BspDetailEtatsdeventesCard;
  };
  global_health: {
    score: number;
    status: 'good' | 'warning' | 'critical';
  };
  alerts: DashboardAlert[];
  period: {
    date_from: string;
    date_to: string;
  };
  generated_at: string;
}
