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>>;
  };
}

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;
}

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;
  number: number;
  name: string;
  comment?: string;
  params: ReportParams;
  auto?: ReportAuto;
  parentId: string;
}

export enum reportAccessScope {
  observerRF = 'observerRF',
  observerRegion = 'observerRegion',
  observerOrganization = 'observerOrganization',
}

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

const queryDefinitionIds: Dictionary<Dictionary<QueryDefinitionIdMatcher>> = {
  1: {
    '46e78d66-41de-4ad7-91b4-118cfe7056b6': (report) =>
      report.params?.organizations?.length === 1 &&
      report.params?.regions?.length === 1,
    '20255492-8c05-4ad3-8029-986f14b780f0': (report) =>
      Number(report.params?.organizations?.length) > 1 &&
      report.params?.regions?.length === 1,
    '1b06b5d2-3b6a-4452-ab48-581b6b380bd2': (report) =>
      Number(report.params?.regions?.length) > 1,
  },
  2: {
    '265159f4-cb85-44dd-a6a5-c7697f5c3c22': (report) =>
      Number(report.params?.organizations?.length) > 0 &&
      report.params?.regions?.length === 1,
    'f5c09354-d343-43d7-b563-afe72ac2bbe4': (report) =>
      report.params?.regions?.length === 1 &&
      !report.params?.organizations?.length 
  },
  3: {
    '773ab9d2-b167-4659-94e9-6dacc1afc60b': () => true,
  },
  4: {
    '5b2f7370-8cb1-4985-8e5d-fea69e1dba4a': () => true,
  },
  6: {
    '6177d1e5-7f2d-468f-ba71-5c305f7b369f': () => true,
  },
  7: {
    '2a450cb6-c0e0-4b22-99a7-c5f6068fbafe': (report) => 
      Number(report.params?.organizations?.length) > 0,
    'a678a828-3786-403e-9d48-0b6ee9e62c48': (report) =>
      !report.params?.organizations?.length &&
      Number(report.params?.regions?.length) > 0,
  },
  9: {
    '012f3638-5d3a-432a-b23b-d6c7ef6ef895': () => true,
  },
  10: {
    '517a75ed-9601-470b-9bc0-13b8e79419f1': () => true,
  },
  11: {
    'b60a0f1b-d8e5-4274-b5ed-5d506654a536': (_, role) =>
      role === reportAccessScope.observerRF,
    'a69b0efe-c9f0-4ace-9f35-81d2979eb534 ': (_, role) =>
      role === reportAccessScope.observerRegion,
    'd1bcb687-6941-4583-b531-67da3ba76449': (_, role) =>
      role === reportAccessScope.observerOrganization
  },
  12: {
    '46ebcaf5-c842-4eb6-9a28-54c2203cc214': (_, role) =>
      role === reportAccessScope.observerRF,
    '57c9b287-fd02-42ec-b366-9fbce17b3726': (_, role) =>
      role === reportAccessScope.observerRegion,
    '2b3dc26f-008a-4ec3-9afd-41c557066b95': (_, role) =>
      role === reportAccessScope.observerOrganization
  },  
  13: {
    'f30fd205-14cd-4583-8a0b-ae60e361da29': (_, role) =>
      role === reportAccessScope.observerRF,
    'aeb3d8b4-face-44a2-a7d3-2efb6ff7fa86': (_, role) =>
      role === reportAccessScope.observerRegion,
    '9fe788cc-c47b-4882-94b0-877231ab8382': (_, role) =>
      role === reportAccessScope.observerOrganization 
  },
  14: {
    'a7593be0-a016-4a40-bb35-9bd268983cd7': () => true
  },
  15: {
    '3a686701-0fe3-43ac-8df1-02395ed7dbc6': () => true
  },
  16: {
    '73b4e26f-f0f9-40a7-96de-974500bb1ced': () => true
  },
  17: {
    'be3d3241-184a-476a-9f70-f6b702d13aaa': () => true
  },
  18: {
    '5f1e693e-65de-4d8c-a9df-776d81458998': () => true
  },
  19: {
    '9a900f3d-b6e6-4299-8e6b-bcb4f8d43830': () => true
  },
  20: {
    'c1ed64ac-2f10-4ba2-ad4d-8b72ead12cf9': () => true
  }
};

export default function findQueryDefinitionId(
  report: Report,
  role?: reportAccessScope
): string | undefined {
  const list = queryDefinitionIds[report.parentId];
  if (!list) {
    return;
  }
  for (const id in list) {
    if (list[id](report, role)) {
      return id;
    }
  }
}
