import { Body, Controller, Get, Post } from "@nestjs/common";

import { {{ module_class_name }}Service } from "./passwordless.service";

type IssuePayload = {
  identifier: string;
  deliveryMethod?: string;
  metadata?: Record<string, unknown>;
};

type VerifyPayload = {
  identifier: string;
  code: string;
  deliveryMethod?: string;
};

@Controller("passwordless")
export class {{ module_class_name }}Controller {
  constructor(private readonly service: {{ module_class_name }}Service) {}

  @Get("metadata")
  getMetadata(): Record<string, unknown> {
    return this.service.describe();
  }

  @Get("features")
  getFeatures(): { features: string[] } {
    return { features: this.service.listFeatures() };
  }

  @Get("delivery-methods")
  listDeliveryMethods(): { deliveryMethods: string[] } {
    return { deliveryMethods: this.service.listDeliveryMethods() };
  }

  @Post("tokens")
  issueToken(@Body() payload: IssuePayload): Record<string, unknown> {
    const token = this.service.issueCode(
      payload.identifier,
      payload.deliveryMethod,
      payload.metadata ?? {},
    );
    return {
      tokenId: token.tokenId,
      deliveryMethod: token.deliveryMethod,
      expiresAt: token.expiresAt,
    };
  }

  @Post("magic-link")
  issueMagicLink(@Body() payload: IssuePayload): Record<string, unknown> {
    const issued = this.service.issueMagicLink(
      payload.identifier,
      payload.deliveryMethod,
      payload.metadata ?? {},
    );
    return issued;
  }

  @Post("verify")
  verifyCode(@Body() payload: VerifyPayload): Record<string, unknown> {
    const token = this.service.verifyCode(
      payload.identifier,
      payload.code,
      payload.deliveryMethod,
    );
    return {
      tokenId: token.tokenId,
      identifier: token.identifier,
      metadata: token.metadata,
    };
  }
}
