import { Controller, Post, Req, Body, HttpCode } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { AppStoreTransactionService } from './app-store-transaction.service';

/**
 * M-MEMBER-01-05: App Store 交易提交端点
 *
 * POST /membership/apple/transactions
 * 仅接受 signedTransactionInfo。客户端禁止提供 userId/isPremium/planCode 等作为权威。
 */

@ApiTags('membership')
@Controller('membership/apple')
export class AppStoreMembershipController {
  constructor(private readonly txService: AppStoreTransactionService) {}

  @Post('transactions')
  @HttpCode(200)
  @ApiOperation({ summary: '提交 App Store 已验证交易' })
  async submitTransaction(
    @Req() req: any,
    @Body() body: { signedTransactionInfo: string },
  ) {
    const result = await this.txService.submitTransaction(
      req.user.id,
      body.signedTransactionInfo,
    );

    return {
      transactionId: result.transactionId,
      status: result.status,
    };
  }
}
