import { z } from '@frontmcp/lazy-zod';
import { Tool, ToolContext } from '@frontmcp/sdk';

import { resolveDemoSessionId } from '../../resolve-session-id';
import { getVault } from '../data/vault.store';

const inputSchema = {
  entryId: z.string().describe('Vault entry ID to delete'),
};

const outputSchema = z
  .object({
    success: z.boolean(),
    message: z.string(),
  })
  .strict();

@Tool({
  name: 'delete-vault-entry',
  description: 'Delete an authorization vault entry',
  inputSchema,
  outputSchema,
})
export default class DeleteVaultEntryTool extends ToolContext {
  async execute(input: z.infer<z.ZodObject<typeof inputSchema>>): Promise<z.infer<typeof outputSchema>> {
    const ctx = this.tryGetContext();
    const sessionId = resolveDemoSessionId(ctx?.sessionId, this.getAuthInfo().sessionId);
    const vault = await getVault(sessionId);

    try {
      await vault.delete(input.entryId);

      return {
        success: true,
        message: `Deleted vault entry ${input.entryId}`,
      };
    } catch (error) {
      return {
        success: false,
        message: `Failed to delete vault entry: ${error instanceof Error ? error.message : 'Unknown error'}`,
      };
    }
  }
}
