import { Controller, ForbiddenException, Get, UseGuards } from '@nestjs/common'
import { ActorService } from '../auth/actor.service.js'
import { CurrentUser } from '../auth/current-user.decorator.js'
import { JwtAuthGuard } from '../auth/jwt-auth.guard.js'
import type { AuthentikJwtPayload } from '../auth/jwt-payload.interface.js'
import { BillingService } from './billing.service.js'

// Partner-facing billing reads. Scoped to the caller's partnerId.
@Controller('me/partner/billing')
@UseGuards(JwtAuthGuard)
export class PartnerBillingController {
  constructor(
    private readonly billing: BillingService,
    private readonly actor: ActorService,
  ) {}

  @Get('summary')
  async summary(@CurrentUser() jwt: AuthentikJwtPayload) {
    const actor = await this.actor.resolve(jwt)
    if (!actor.partnerId) throw new ForbiddenException('Not a partner-staff user')
    return this.billing.partnerSummary(actor.partnerId)
  }

  @Get('invoices')
  async invoices(@CurrentUser() jwt: AuthentikJwtPayload) {
    const actor = await this.actor.resolve(jwt)
    if (!actor.partnerId) throw new ForbiddenException('Not a partner-staff user')
    return this.billing.partnerInvoices(actor.partnerId)
  }

  @Get('payouts')
  async payouts(@CurrentUser() jwt: AuthentikJwtPayload) {
    const actor = await this.actor.resolve(jwt)
    if (!actor.partnerId) throw new ForbiddenException('Not a partner-staff user')
    return this.billing.partnerPayouts(actor.partnerId)
  }
}
