import { createResolver, t } from "@tailor-platform/sdk";
import { createContext } from "@tailor-platform/erp-kit/app";
import { getDB } from "@/generated/kysely-tailordb";
import { umModules, auditModules } from "@/modules";

export default createResolver({
  name: "revokeRoleFromUser",
  operation: "mutation",
  input: {
    userId: t.string().description("User ID"),
    roleId: t.string().description("Role ID to revoke"),
  },
  body: async (context) => {
    const db = getDB("main-db");
    const result = await db.transaction().execute(async (trx) => {
      const cmdResult = await umModules.commands.revokeRoleFromUser(
        trx,
        {
          userId: context.input.userId,
          roleId: context.input.roleId,
        },
        createContext(context),
      );
      if (!cmdResult.ok) {
        switch (cmdResult.error.code) {
          case "USER_MANAGEMENT_USER_NOT_FOUND":
            throw new Error(`User ${context.input.userId} does not exist`);
          case "USER_MANAGEMENT_ROLE_NOT_FOUND":
            throw new Error(`Role ${context.input.roleId} does not exist`);
          case "USER_MANAGEMENT_ASSIGNMENT_NOT_FOUND":
            throw new Error("Role is not assigned to this user");
          case "INSUFFICIENT_PERMISSION":
            throw cmdResult.error;
        }
      }
      return {
        userId: context.input.userId,
        roleId: context.input.roleId,
      };
    });

    await db
      .transaction()
      .execute(async (trx) => {
        await auditModules.commands.logAuditEvent(
          trx,
          {
            eventId: crypto.randomUUID(),
            actorType: "USER",
            actorId: context.user.id,
            entityType: "UserRole",
            entityId: `${result.userId}:${result.roleId}`,
            operationType: "DELETE",
            changes: [
              { fieldName: "userId", oldValue: result.userId },
              { fieldName: "roleId", oldValue: result.roleId },
            ],
          },
          createContext(context),
        );
      })
      .catch(() => undefined);

    return result;
  },
  output: t
    .object({
      userId: t.string().description("User ID"),
      roleId: t.string().description("Revoked role ID"),
    })
    .description("Role revocation result"),
});
