import { Controller, Get, Param, Post } from '@nestjs/common';
import { PrismaService } from '../common/prisma.service';
import { getMerchant } from '../common/core';

@Controller('admin')
export class FraudController {
  constructor(private prisma: PrismaService) {}

  // --- Fraud flags (PRD 4.4) ---
  @Get('fraud')
  async fraud() {
    const m = await getMerchant(this.prisma);
    const flags = await this.prisma.fraudFlag.findMany({
      where: { merchantId: m.id, resolved: false },
      orderBy: { createdAt: 'desc' },
      include: { affiliate: { select: { name: true } } },
    });
    return flags.map((f) => ({
      id: f.id,
      affiliate: f.affiliate?.name ?? '—',
      reason: f.reason,
      ip: f.ipAddress,
      date: f.createdAt.toISOString().slice(0, 10),
    }));
  }

  @Post('fraud/:id/resolve')
  async resolveFraud(@Param('id') id: string) {
    await this.prisma.fraudFlag.update({ where: { id }, data: { resolved: true } });
    return { ok: true };
  }
}
