/** Core entity configuration — drives all generic CRM components */
export interface EntityConfig {
  entityType: string;
  apiPrefix: string;
  label: { singular: string; plural: string; genitive: string };
  systemFields: FieldDef[];
  features: {
    pipelines: boolean;
    activities: boolean;
    timeline: boolean;
    customFields: boolean;
  };
}

export interface FieldDef {
  key: string;
  label: string;
  type: 'string' | 'number' | 'currency' | 'date' | 'enum' | 'relation' | 'phone' | 'email';
  section?: string;
  enumOptions?: { value: string; label: string }[];
  relationEndpoint?: string;
  defaultVisible?: boolean;
  defaultInCard?: boolean;
}

/** Stage data shared across components */
export interface StageData {
  id: string;
  name: string;
  color: string;
  type: string;
  position: number;
  staleDays?: number | null;
  isDefault: boolean;
}

/** Pipeline data */
export interface PipelineData {
  id: string;
  name: string;
  isDefault: boolean;
  isActive: boolean;
  entityType?: string;
  stages: StageData[];
}

/** Minimal entity card data for kanban/table */
export interface EntityCardData {
  id: string;
  number?: number | null;
  title: string;
  amount?: number | null;
  stage?: { name: string; color: string; type: string } | null;
  assignee?: { firstName: string; lastName: string } | null;
  createdAt: string;
  [key: string]: any;
}

/** Kanban column */
export interface KanbanColumn {
  stage: StageData;
  items: EntityCardData[];
}

/** Activity */
export interface ActivityData {
  id: string;
  type: string;
  subject: string;
  description?: string;
  scheduledAt: string;
  completedAt?: string | null;
  result?: string | null;
  assignee?: { firstName: string; lastName: string } | null;
  completedBy?: { firstName: string; lastName: string } | null;
}

/** Timeline entry */
export interface TimelineEntryData {
  id: string;
  type: string;
  subject?: string;
  content?: string;
  metadata?: Record<string, string>;
  pinnedAt?: string | null;
  user?: { id: string; firstName: string; lastName: string } | null;
  createdAt: string;
}

/** Custom field definition */
export interface CustomFieldDef {
  id: string;
  fieldName: string;
  name: string;
  type: string;
  isRequired: boolean;
  isMultiple: boolean;
  showToTrainer: boolean;
  listOptions?: string[] | null;
  description?: string | null;
  position: number;
  defaultValue?: string | null;
}

/** API adapter — injected via CrmProvider */
export interface CrmApi {
  get<T = any>(url: string): Promise<T>;
  post<T = any>(url: string, data?: any): Promise<T>;
  patch<T = any>(url: string, data?: any): Promise<T>;
  put<T = any>(url: string, data?: any): Promise<T>;
  delete<T = any>(url: string): Promise<T>;
}
