import react from "eslint-plugin-react";
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import pluginQuery from "@tanstack/eslint-plugin-query";
import kyselyRules from "./kyselyRules/plugin.js";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import noRelativeImportPaths from "eslint-plugin-no-relative-import-paths";
import { FlatCompat } from "@eslint/eslintrc";
import jsxA11y from "eslint-plugin-jsx-a11y";
import eslintPluginBetterTailwindcss from "eslint-plugin-better-tailwindcss";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: js.configs.recommended,
  allConfig: js.configs.all,
});

export default [
  ...pluginQuery.configs["flat/recommended"],

  {
    ignores: [
      "**/.next",
      "**/out",
      "**/public",
      "**/node_modules/",
      "**/check-env-vars.js",
      "**/headers/",
      "**/schemas/",
      "**/kyselyRules/",
      "prettier.config.cjs",
      "postcss.config.cjs",
      ".kanelrc.js",
    ],
  },
  ...compat.extends(
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@next/next/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-type-checked",
    "plugin:@typescript-eslint/stylistic-type-checked",
    "plugin:jsx-a11y/recommended",
  ),
  {
    plugins: {
      react,
      "@typescript-eslint": typescriptEslint,
      "better-tailwindcss": eslintPluginBetterTailwindcss,
      "jsx-a11y": jsxA11y,
      kyselyRules: kyselyRules,
      //"no-relative-import-paths": noRelativeImportPaths,
    },

    languageOptions: {
      parser: tsParser,
      ecmaVersion: 5,
      sourceType: "script",

      parserOptions: {
        project: true,
        tsconfigRootDir: "./",
      },
    },
    linterOptions: {
      reportUnusedDisableDirectives: true,
    },

    settings: {
      react: {
        version: "detect",
      },

      "import/resolver": {
        typescript: {
          alwaysTryTypes: true,
          project: "./tsconfig.json",
        },
      },
      "better-tailwindcss": {
        entryPoint: "src/styles/globals.css",
      },
    },

    rules: {
      "react/react-in-jsx-scope": "off",
      "react-hooks/exhaustive-deps": "off",
      "react/prop-types": "off",
      "@typescript-eslint/ban-types": "off",
      "@typescript-eslint/ban-ts-comment": "off",
      "@typescript-eslint/array-type": "off",
      "@typescript-eslint/no-redundant-type-constituents": "warn",
      "@typescript-eslint/consistent-type-definitions": "off",
      "@typescript-eslint/prefer-optional-chain": "off",
      "@typescript-eslint/prefer-nullish-coalescing": "off",
      "@typescript-eslint/no-empty-interface": "off",
      "@typescript-eslint/unbound-method": "off",
      "@typescript-eslint/consistent-type-imports": [
        "warn",
        {
          prefer: "type-imports",
          fixStyle: "inline-type-imports",
        },
      ],

      "@typescript-eslint/no-unused-vars": [
        "warn",
        {
          argsIgnorePattern: "^_",
        },
      ],

      "@typescript-eslint/require-await": "off",
      "@typescript-eslint/switch-exhaustiveness-check": "warn",

      "@typescript-eslint/no-misused-promises": [
        "error",
        {
          checksVoidReturn: {
            attributes: false,
          },
        },
      ],

      "@next/next/no-img-element": "warn",
      "kyselyRules/enforce-select": "error",
      "kyselyRules/enforce-where": "error",
      "kyselyRules/enforce-null": "error",
      ...eslintPluginBetterTailwindcss.configs["recommended-warn"]?.rules,
      ...eslintPluginBetterTailwindcss.configs["recommended-error"]?.rules,
      "better-tailwindcss/enforce-consistent-line-wrapping": "off",
      "better-tailwindcss/enforce-consistent-class-order": "off",
    },
  },
  {
    //ignores: ["**/emails/**/*.tsx", "next.config.js"],
    files: ["src/**/*.tsx", "src/**/*.ts"],
    plugins: {
      "no-relative-import-paths": noRelativeImportPaths,
    },
    rules: {
      "no-relative-import-paths/no-relative-import-paths": [
        "warn",
        { rootDir: "src", prefix: "~" },
      ],
    },
  },
];
