import { parsePayload } from "./jwt";

export const Roles = {
  READ: process.env.REACT_APP_ROLE_READ_ONLY,
  CREATE: process.env.REACT_APP_ROLE_CREATE,
  DELETE: process.env.REACT_APP_ROLE_DELETE,
  CERTIFICATES: process.env.REACT_APP_ROLE_GENERATE_CERTIFICATE,
  APPROVESUPPLIERS: process.env.REACT_APP_ROLE_SUPPLIER_APPROVER,
  UPDATEDEPLOY: process.env.REACT_APP_ROLE_UPDATE_DEPLOY,
  MANUFACTURE: process.env.REACT_APP_ROLE_MANUFACTURE,
  MAGNAGROUP: process.env.REACT_APP_MAGNA_GROUP_ID,
  MANIFEST_MIGRATION: process.env.REACT_APP_ROLE_MANIFEST_MIGRATION,
  CAR_DIAGNOSTIC: process.env.REACT_APP_ROLE_CAR_DIAGNOSTIC
};

export const Providers = {
  FISKER: "Fisker",
  FISKER_QA: "Fisker-QA",
  MAGNA: process.env.REACT_APP_MAGNA_PROVIDER,
};

export const getGroups = (token) => {
  const payload = parsePayload(token);

  if (!payload || !payload["custom:groups"]) return null;

  return payload["custom:groups"];
};

export const getProviders = (token) => {
  const payload = parsePayload(token);

  if (!payload || !payload["identities"] || payload["identities"].length === 0)
    return null;

  return payload["identities"].map((identity) => identity["providerName"]);
};

export const hasRole = (userGroups, rolesPerProvider, userProviders) => {
  if (!rolesPerProvider || rolesPerProvider.length === 0) return true;

  if (!userGroups || userGroups.length === 0) return false;

  if (rolesPerProvider && userProviders) {
    for (const provider of userProviders) {
      if (rolesPerProvider[provider]) {
        const rolesForProvider = rolesPerProvider[provider];
        if (!rolesForProvider || rolesForProvider.length === 0) return true;
        if (
          rolesForProvider.filter((role) => userGroups.includes(role)).length >
          0
        ) {
          return true;
        }
      }
    }
  }

  return false;
};


export const Permissions = {
  FiskerRead: {
    [Providers.FISKER]: [Roles.CREATE, Roles.READ],
  },
  FiskerCreate: {
    [Providers.FISKER]: [Roles.CREATE],
  },
  FiskerDelete: {
    [Providers.FISKER]: [Roles.DELETE],
  },
  FiskerSupplierAdmin: {
    [Providers.FISKER]: [Roles.APPROVESUPPLIERS],
  },
  FiskerMagnaCertificate: {
    [Providers.FISKER]: [Roles.CERTIFICATES],
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  FiskerTools: {
    [Providers.FISKER]: [Roles.CERTIFICATES, Roles.CREATE],
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  FiskerUpdateDeploy: {
    [Providers.FISKER]: [Roles.UPDATEDEPLOY],
  },
  Magna: {
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  FiskerMagnaRead: {
    [Providers.FISKER]: [Roles.CREATE, Roles.READ],
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  FiskerMagnaCreate: {
    [Providers.FISKER]: [Roles.CREATE],
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  FiskerMagnaDelete: {
    [Providers.FISKER]: [Roles.DELETE],
    [Providers.FISKER_QA]: [Roles.MANUFACTURE],
    [Providers.MAGNA]: [Roles.MAGNAGROUP],
  },
  ManifestMigration: {
    [Providers.FISKER]: [Roles.MANIFEST_MIGRATION]
  },
  CarDiagnostic: {
    [Providers.FISKER]: [Roles.CAR_DIAGNOSTIC],
    [Providers.FISKER_QA]: [Roles.CAR_DIAGNOSTIC],
  }
};
