// Models pour les réponses API
export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
  errors?: string[];
}

// Models pour les menus
export interface Menu {
  id: number;
  name: string;
  icon: string;
  labs: string;
  enabled: boolean;
  subs: SubMenu[];
}

export interface SubMenu {
  name: string;
  route: string;
  module: string;
  source: 'data' | 'backoffice';
  icon?: string;
}

// Models pour les métadonnées
export interface Metadata {
  CreateView: {
    fields: Field[];
  };
  ListView: {
    list: ListView[];
    cta: CTA[];
    badges: Badge[];
  };
  DetailView: {
    fields: Field[];
    headers: Header[];
  };
  UpdateView: {
    fields: Field[];
  };
}

export interface ListView {
  name: string;
  displayName?: string;
  fields: Field[];
}

export interface Field {
  name: string;
  fieldname: string;
  displayName: string;
  type: FieldType;
  mandatory: boolean;
  minWidth?: number;
  sequencelist?: number;
  sequencecreate?: number;
  sequencedetail?: number;
  sequenceupdate?: number;
  bloc?: string;
  bloc_enabled?: boolean;
  module?: string;
  options?: PicklistOption[];
  reference?: string;
}

export type FieldType = 
  | 'string'
  | 'text'
  | 'number'
  | 'integer'
  | 'double'
  | 'currency'
  | 'date'
  | 'datetime'
  | 'timestamp'
  | 'boolean'
  | 'picklist'
  | 'reference'
  | 'file'
  | 'email'
  | 'url'
  | 'phone';

export interface PicklistOption {
  value: string;
  label: string;
  color_back?: string;
  color_font?: string;
}

export interface Header {
  name: string;
  displayName: string;
  type: FieldType;
}

// Models pour les CTAs (Call To Action)
export interface CTA {
  action: CTAAction;
  label: string;
  name: string;
  type: 'button' | 'link';
  icon?: string;
  color?: string;
  permission?: string;
}

export type CTAAction = 
  | 'create'
  | 'update'
  | 'delete'
  | 'execute'
  | 'report'
  | 'export'
  | 'import'
  | 'refresh'
  | 'custom';

// Models pour les badges/KPIs
export interface Badge {
  id: number;
  name: string;
  title: string;
  description: string;
  icon: string;
  type: BadgeType;
  field?: string;
  value?: any;
  formatted_value?: string;
  condition?: any;
  result?: number;
  color?: string;
  config?: BadgeConfig;
  last_updated?: string;
  source?: 'estair_api' | 'chaps' | 'fallback';
  error?: string;
}

export interface BadgeConfig {
  format?: 'number' | 'currency' | 'percentage';
  color?: string;
  endpoint?: string;
}

export interface DashboardBadge {
  success: boolean;
  data: Badge[];
  meta: {
    count: number;
    timestamp: string;
    communication: string;
  };
}

export interface BadgeHealthCheck {
  success: boolean;
  status: 'healthy' | 'degraded' | 'error';
  tests: {
    database_isolation: {
      passed: boolean;
      message: string;
    };
    api_communication: {
      passed: boolean;
      latency_ms: number;
      message: string;
    };
    badges_available: {
      passed: boolean;
      count: number;
      message: string;
    };
  };
  architecture: {
    chaps_database: string;
    estair_communication: string;
    security: string;
  };
  timestamp: string;
}

export type BadgeType = 
  | 'count'
  | 'sum'
  | 'average'
  | 'percentage'
  | 'custom';

// Models pour les domaines
export interface Domain {
  id: number;
  name: string;
  label: string;
  icon: string;
  description?: string;
  is_active: boolean;
}

// Models pour les modules
export interface Module {
  id: number;
  name: string;
  label: string;
  source: 'data' | 'backoffice';
  description?: string;
  is_deleted: boolean;
}

// Models pour l'utilisateur
export interface User {
  id: number;
  uuid: string;
  name: string;
  email: string;
  status: string;
  office?: string;
  manager?: number;
  roles: Role[];
  permissions: Permission[];
}

export interface Role {
  id: number;
  name: string;
  label: string;
  permissions: Permission[];
}

export interface Permission {
  id: number;
  name: string;
  label: string;
  resource: string;
  action: string;
}

// Models pour les comptes (exemple d'entité métier)
export interface Account {
  id: number;
  uuid: string;
  name: string;
  office_id?: string;
  estair_id?: string;
  address?: string;
  email?: string;
  city?: string;
  postal_code?: string;
  telephone?: string;
  country?: string;
  balance: number;
  daily_credit: number;
  outstanding: number;
  exposition: number;
  limit: number;
  type: string;
  status: string;
  assigned_to?: number;
  sector?: string;
  rts_active: boolean;
  description?: string;
  created_at: Date;
  updated_at: Date;
}

// Models pour les états de vente
export interface EtatDeVente {
  id: number;
  uuid: string;
  account_id: number;
  pnr: string;
  date: Date;
  status: string;
  type: string;
  amount: number;
  commission: number;
  tax: number;
  created_at: Date;
  updated_at: Date;
}

// Types utilitaires
export interface TableColumn {
  field: string;
  headerName: string;
  type: FieldType;
  width?: number;
  sortable?: boolean;
  filterable?: boolean;
  editable?: boolean;
  cellRenderer?: string;
  valueFormatter?: string;
}

export interface FilterOptions {
  field: string;
  operator: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'greaterThan' | 'lessThan';
  value: any;
}

export interface SortOptions {
  field: string;
  direction: 'asc' | 'desc';
}

export interface PaginationOptions {
  page: number;
  pageSize: number;
  total?: number;
}

