import {
  defineConfig,
  defineAuth,
  defineIdp,
  defineStaticWebSite,
  definePlugins,
} from "@tailor-platform/sdk";
import { kyselyTypePlugin } from "@tailor-platform/sdk/plugin/kysely-type";
import { seedPlugin } from "@tailor-platform/sdk/plugin/seed";
import { user } from "@/modules-db";

const idp = defineIdp("default", {
  authorization: "loggedIn",
  clients: ["default"],
  userAuthPolicy: {
    allowSelfPasswordReset: true,
  },
  gqlOperations: "query",
});

const website = defineStaticWebSite("default-web", {
  description: "Default static website",
});

const auth = defineAuth("default", {
  userProfile: {
    type: user,
    usernameField: "email",
    attributes: {
      permissions: true,
    },
  },
  machineUsers: {
    developer: {
      attributes: {
        permissions: [
          "user-management:createUser",
          "user-management:activateUser",
          "user-management:deactivateUser",
          "user-management:reactivateUser",
          "user-management:createRole",
          "user-management:assignPermissionToRole",
          "user-management:revokePermissionFromRole",
          "user-management:assignRoleToUser",
          "user-management:revokeRoleFromUser",
          "user-management:createPermission",
          "user-management:updateUser",
          "user-management:updateOwnProfile",
          "audit:logAuditEvent",
        ],
      },
    },
  },
  oauth2Clients: {
    default: {
      redirectURIs: ["http://localhost:5173", website.url],
      grantTypes: ["authorization_code", "refresh_token"],
      clientType: "public",
      requireDpop: true,
    },
  },
  idProvider: idp.provider(idp.name, "default"),
});

export default defineConfig({
  name: "template-app",
  cors: ["http://localhost:5173", website.url],
  db: {
    "main-db": {
      files: [`./src/modules-db.ts`],
      gqlOperations: "query",
    },
  },
  resolver: {
    "main-resolver": { files: [`./src/resolvers/**/*.ts`] },
  },
  executor: { files: [`./src/executors/**/*.ts`] },
  auth: auth,
  idp: [idp],
  staticWebsites: [website],
});

export const plugins = definePlugins(
  kyselyTypePlugin({ distPath: "./src/generated/kysely-tailordb.ts" }),
  seedPlugin({ distPath: "./seed", machineUserName: "developer" }),
);
