import { Logging } from "./logging"
import { TokenType } from "@prisma/client"

export interface rejectionError {
  rejected?: boolean
  message: string
  statusCode: number
  solved: boolean
  type?: Logging
}
interface rejectionErrors {
  [key: string]: rejectionError
}
interface rejectionErrorFns {
  [key: string]: (...args: any[]) => rejectionError
}

export const rejectionErrors: rejectionErrors = {
  noCookie: {
    rejected: true,
    message: "Unauthorized. No cookie.",
    statusCode: 401,
    solved: true,
  },
  noBody: {
    rejected: true,
    message: "Unauthorized. No Body.",
    statusCode: 401,
    solved: true,
    type: "warn",
  },
  noUsername: {
    rejected: true,
    message: "No username in request body!",
    statusCode: 401,
    solved: true,
    type: "warn",
  },
  noPassword: {
    rejected: true,
    message: "No password in request body!",
    statusCode: 401,
    solved: true,
    type: "warn",
  },
  wrongPassword: {
    message: "Passwords do not match!",
    statusCode: 401,
    solved: true,
    type: "warn",
  },
  playerNotFound: {
    message: "Player name not found in DB!",
    statusCode: 401,
    solved: false,
    type: "warn",
  },
  tokenUsed: {
    rejected: true,
    message: "DBToken was already used!",
    statusCode: 401,
    solved: true,
    type: "warn",
  },
  registered: {
    rejected: true,
    message: "Player is already registered!",
    statusCode: 403,
    solved: true,
    type: "warn",
  },
  gameNotFound: {
    rejected: true,
    message: "Game not found!",
    statusCode: 403,
    solved: true,
  },
}

export const rejectionErrorFns: rejectionErrorFns = {
  tokenWasString(tokenType: TokenType, tokenValue: string) {
    return {
      rejected: true,
      message: `${tokenType}-Token data was a string. Token: ${tokenValue}`,
      statusCode: 401,
      solved: false,
    }
  },
  tokenNotFound(tokenType: TokenType) {
    return {
      rejected: true,
      message: `${tokenType}-Token not found in DB!`,
      statusCode: 401,
      solved: true,
      type: "warn",
    }
  },
  noToken(tokenType: TokenType) {
    return {
      rejected: true,
      message: `Unauthorized. No ${tokenType}-Token.`,
      statusCode: 401,
      solved: true,
    }
  },
}
