export type ReportTabKey = "financial" | "sales" | "products" | "customers" | "custom";

export type ReportExportFormat = "csv" | "xlsx" | "pdf" | "print";
export type ReportSortDirection = "asc" | "desc";
export type ReportColumnType =
  | "text"
  | "number"
  | "currency"
  | "percent"
  | "date"
  | "badge";

export type ReportFilterValue = string | string[] | null;

export type ReportFilterOption = {
  value: string;
  label: string;
};

export type ReportSort = {
  columnId: string;
  direction: ReportSortDirection;
} | null;

export type ReportTableColumn = {
  id: string;
  label: string;
  type: ReportColumnType;
  width?: number;
  minWidth?: number;
  align?: "left" | "center" | "right";
  sortable?: boolean;
  visibleByDefault?: boolean;
};

export type ReportTableRow = Record<string, string | number | null>;

export type ReportKpi = {
  id: string;
  label: string;
  value: number;
  type: "currency" | "number" | "percent";
  tone?: "default" | "positive" | "warning" | "danger";
};

export type ReportChartType = "line" | "bar" | "column" | "donut" | "area" | "ranking" | "compare";

export type ReportChartSeriesPoint = {
  label: string;
  value: number;
  secondaryValue?: number;
};

export type ReportChart = {
  id: string;
  title: string;
  type: ReportChartType;
  format: "currency" | "number" | "percent";
  series: ReportChartSeriesPoint[];
};

export type ReportCompanyInfo = {
  companyId: string;
  companyName: string;
  companyDocument: string | null;
  companyLogoUrl: string | null;
};

export type ReportCustomUniverse =
  | "sales"
  | "products"
  | "customers"
  | "financialCash"
  | "financialReceivables";

export type ReportCustomField = {
  id: string;
  label: string;
  type: ReportColumnType;
  tabKeys: ReportTabKey[];
  universes?: ReportCustomUniverse[];
};

export type ReportCustomMetric = {
  id: string;
  label: string;
  type: "currency" | "number" | "percent";
  universes: ReportCustomUniverse[];
};

export type ReportCustomConfig = {
  name?: string;
  universe: ReportCustomUniverse;
  columns: string[];
  metrics: string[];
  filters: Record<string, ReportFilterValue>;
  groupBy: string | null;
  sort: ReportSort;
};

export type ReportPreset = {
  id: string;
  name: string;
  reportKey: ReportTabKey;
  scope: "company" | "user";
  isFavorite: boolean;
  configVersion: number;
  config: ReportCustomConfig | Record<string, ReportFilterValue>;
  createdAt: string;
  updatedAt: string;
};

export type ReportRequest = {
  tab: ReportTabKey;
  filters: Record<string, ReportFilterValue>;
  page: number;
  pageSize: number;
  search: string;
  sort: ReportSort;
  visibleColumnIds?: string[];
  custom?: ReportCustomConfig | null;
  exportAllRows?: boolean;
  skipAudit?: boolean;
};

export type ReportOptionsPayload = {
  company: ReportCompanyInfo;
  paymentMethods: ReportFilterOption[];
  statuses: ReportFilterOption[];
  financialPaymentMethods: ReportFilterOption[];
  financialStatuses: ReportFilterOption[];
  bankAccounts: ReportFilterOption[];
  financialCategories: ReportFilterOption[];
  channels: ReportFilterOption[];
  clients: ReportFilterOption[];
  sellers: ReportFilterOption[];
  categories: ReportFilterOption[];
  suppliers: ReportFilterOption[];
  cities: ReportFilterOption[];
  states: ReportFilterOption[];
  presets: ReportPreset[];
  customFields: ReportCustomField[];
  customMetrics: ReportCustomMetric[];
};

export type ReportResponse = {
  tab: ReportTabKey;
  title: string;
  generatedAt: string;
  company: ReportCompanyInfo;
  appliedFilters: Record<string, ReportFilterValue>;
  kpis: ReportKpi[];
  charts: ReportChart[];
  columns: ReportTableColumn[];
  rows: ReportTableRow[];
  totalRows: number;
  page: number;
  pageSize: number;
};

export type ReportPresetInput = {
  id?: string;
  name: string;
  reportKey: ReportTabKey;
  scope: "company" | "user";
  isFavorite?: boolean;
  config: ReportCustomConfig | Record<string, ReportFilterValue>;
};

export type ReportAuditEventType = "generate" | "export";
export type ReportAuditFormat = "screen" | ReportExportFormat;
