import { z } from '@frontmcp/lazy-zod';
import { Resource, ResourceContext } from '@frontmcp/sdk';

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

const outputSchema = z.object({
  sessionId: z.string(),
  keys: z.array(z.string()),
  data: z.record(z.string(), z.string()),
});

@Resource({
  uri: 'session://current',
  name: 'Current Session',
  description: 'Current session data',
  mimeType: 'application/json',
})
export default class SessionCurrentResource extends ResourceContext<
  Record<string, never>,
  z.infer<typeof outputSchema>
> {
  async execute(): Promise<z.infer<typeof outputSchema>> {
    // Prefer FrontMcpContext.sessionId (always available in public mode) over authInfo.sessionId
    const ctx = this.tryGetContext();
    const sessionId = resolveDemoSessionId(ctx?.sessionId, this.getAuthInfo().sessionId);
    const store = getSessionStore(sessionId);

    const data = store.getAll();

    return {
      sessionId,
      keys: Object.keys(data),
      data,
    };
  }
}
