import { HttpStatus } from "http-status";
import { paths } from "./liljudd";

export type MyPaths = keyof paths;

export type Methods<Path extends MyPaths> = keyof paths[Path];

export type Responses<
  Path extends MyPaths,
  Method extends Methods<Path>,
> = "responses" extends keyof paths[Path][Method]
  ? paths[Path][Method]["responses"]
  : never;

type StatusCodes<P extends MyPaths, M extends Methods<P>> = {
  [CodeName in keyof HttpStatus]: HttpStatus[CodeName] extends number
    ? HttpStatus[CodeName] extends keyof Responses<P, M>
      ? CodeName
      : never
    : never;
}[keyof HttpStatus];

export type ResponseSchemas<
  Path extends MyPaths,
  Method extends Methods<Path>,
  Code extends StatusCodes<Path, Method>,
> = Code extends keyof HttpStatus
  ? HttpStatus[Code] extends keyof Responses<Path, Method>
    ? "content" extends keyof Responses<Path, Method>[HttpStatus[Code]]
      ? "application/json" extends keyof Responses<
          Path,
          Method
        >[HttpStatus[Code]]["content"]
        ? Responses<
            Path,
            Method
          >[HttpStatus[Code]]["content"]["application/json"] extends never
          ? null
          : Responses<
              Path,
              Method
            >[HttpStatus[Code]]["content"]["application/json"]
        : never
      : never
    : never
  : never;

export type Parameters<
  Path extends MyPaths,
  Method extends Methods<Path>,
> = "parameters" extends keyof paths[Path][Method]
  ? "path" extends keyof paths[Path][Method]["parameters"]
    ? paths[Path][Method]["parameters"]["path"]
    : never
  : never;

export type RequestBody<
  Path extends MyPaths,
  Method extends Methods<Path>,
> = "requestBody" extends keyof paths[Path][Method]
  ? "content" extends keyof paths[Path][Method]["requestBody"]
    ? "application/json" extends keyof paths[Path][Method]["requestBody"]["content"]
      ? paths[Path][Method]["requestBody"]["content"]["application/json"]
      : never
    : never
  : never;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface APIResponse<Path extends MyPaths, Method extends Methods<Path>>
  extends Response {}
