import { Dictionary } from 'vue-router/types/router';

export interface BaseReportColumn<N extends string = string> {
  field: string;
  label: string;
  name: N;
  props?: Record<string, unknown>;
}
export interface BaseReportCreateColumn<N extends string = string>
  extends BaseReportColumn<N> {
  component: string;
  linked?: Record<
    string,
    {
      column: N;
      default: unknown;
      value?: string;
    }
  >;
  fieldMap?: string | string[] | Record<string, string>;
  style?: string;
  auto?: boolean;
}

export interface BaseReport {
  id: string;
  name: string;
  columns: {
    create?: Array<string | BaseReportCreateColumn<string>>;
    observerRFCreate?: Array<string | BaseReportCreateColumn<string>>;
    observerRegionCreate?: Array<string | BaseReportCreateColumn<string>>;
    observerOrganizationCreate?: Array<string | BaseReportCreateColumn<string>>;
    search?: Array<string | BaseReportColumn<string>>;
    card?: Array<string | BaseReportColumn<string>>;
    reports?: Array<string | BaseReportColumn<string>>;
    reportsAuto?: Array<string | BaseReportColumn<string>>;
    attachments?: Array<string | BaseReportColumn<string>>;
  };
  source: string;
  queryDefinitions: Dictionary<QueryDefinitionIdMatcher>;
}

export interface ReportRegion {
  id: string;
  code: string;
  name: string;
}

export interface ReportOrganization {
  id: string;
  fullname: string;
  shortname: string;
}

export interface ReportAccount {
  organizationId: string;
  firstname: string;
  middlename: string;
  surname: string;
  fullname: string;
  shortname: string;
  id: string;
}

export interface ReportService {
  id: string;
  fullName: string;
  shortName: string;
}

export interface ReportRange {
  to: Date;
  from: Date;
}

export interface ReportParams {
  regions?: ReportRange[];
  organizations?: ReportOrganization[];
  services?: ReportService[];
  accounts?: ReportAccount[];
  range?: Range;
  allRegions?: boolean;
  allOrganizations?: boolean;
  allServices?: boolean;
  allVks?: boolean;
  vks?: any[];
}

export interface ReportAuto {
  stopped?: ReportAccount & {
    date: Date;
  };
  enabled?: boolean;
  every?: ReportEvery;
}

export enum ReportEvery {
  day = 'day',
  week = 'week',
  month = 'month',
  quarter = 'quarter',
  sixMonths = 'sixMonths',
  year = 'year',
}

export interface Report {
  id?: string;
  queryDefinitionId?: string;
  creator: ReportAccount;
  hidden?: boolean;
  modified?: number;
  schema?: string;
  source: string;
  number: number;
  name: string;
  comment?: string;
  params: ReportParams;
  auto?: ReportAuto;
  parentId: string;
}

export enum reportAccessScope {
  MILITARY_COMMISSAR = 'military_commissar',
  GOMU_SPECIALIST = 'gomu_specialist',
  SYSTEM_ADMINISTRATOR = 'system_administrator',
}

export interface QueryDefinitionIdMatcher {
  (report: Report, role?: reportAccessScope): boolean;
}

export default function findQueryDefinitionId(
  base: BaseReport,
  report: Report,
  role?: reportAccessScope
): string | undefined {
  const list = base.queryDefinitions;
  if (!list) {
    return;
  }
  // TODO: возможно неправильное предположение что ключи сохраняют распорядок
  // но это уже долгое время работало поэтому трогать не буду
  for (const id in list) {
    if (list[id](report, role)) {
      return id;
    }
  }
}
