import {
  Controller,
  Post,
  Get,
  Param,
  Body,
  HttpCode,
  ParseIntPipe,
  UseGuards,
} from '@nestjs/common';
import { BillingService } from './billing.service';
// Superadmin-only lock for every billing admin action.
import { SuperadminGuard } from './superadmin.guard';

/**
 * ADMIN / SUPERADMIN billing controls.
 *
 * Mounted under `companies` so the project's global PermissionGuard protects it
 * exactly like the existing /companies/:id/portal-users admin routes:
 *   - staff with `companies` permission  → allowed
 *   - clients (no staff permission)      → blocked (403)
 *
 * To restrict to superadmin ONLY, add a @RequirePermission(...) decorator or a
 * role check — see the note in the chat.
 */
@Controller('companies')
@UseGuards(SuperadminGuard) // ⚠️ superadmin-only — see SuperadminGuard notes
export class BillingAdminController {
  constructor(private readonly billing: BillingService) {}

  /** GET /api/companies/:companyId/billing — current subscription + admin-set price */
  @Get(':companyId/billing')
  @HttpCode(200)
  async status(@Param('companyId', ParseIntPipe) companyId: number) {
    return { success: true, data: await this.billing.getStatus(companyId) };
  }

  /**
   * POST /api/companies/:companyId/billing/set-price
   * body: { amount: number (minor units), currency?: 'aed', interval?: 'year'|'month'|'one_time' }
   */
  @Post(':companyId/billing/set-price')
  @HttpCode(200)
  async setPrice(
    @Param('companyId', ParseIntPipe) companyId: number,
    @Body() body: { amount: number; currency?: string; interval?: string },
  ) {
    return {
      success: true,
      data: await this.billing.setCompanyPrice(
        companyId,
        Number(body?.amount),
        body?.currency,
        body?.interval,
      ),
    };
  }

  /**
   * POST /api/companies/:companyId/billing/enable
   * Turn the paywall ON for this company — the client portal will now show
   * the payment gateway and block the dashboard/app until they pay.
   */
  @Post(':companyId/billing/enable')
  @HttpCode(200)
  async enable(@Param('companyId', ParseIntPipe) companyId: number) {
    return {
      success: true,
      data: await this.billing.setBillingEnabled(companyId, true),
    };
  }

  /** POST /api/companies/:companyId/billing/disable — turn the paywall OFF */
  @Post(':companyId/billing/disable')
  @HttpCode(200)
  async disable(@Param('companyId', ParseIntPipe) companyId: number) {
    return {
      success: true,
      data: await this.billing.setBillingEnabled(companyId, false),
    };
  }

  /** POST /api/companies/:companyId/billing/grant-free — free access, no charge */
  @Post(':companyId/billing/grant-free')
  @HttpCode(200)
  async grantFree(@Param('companyId', ParseIntPipe) companyId: number) {
    return { success: true, data: await this.billing.grantFreeAccess(companyId) };
  }

  /** POST /api/companies/:companyId/billing/mark-paid — offline/manual payment */
  @Post(':companyId/billing/mark-paid')
  @HttpCode(200)
  async markPaid(@Param('companyId', ParseIntPipe) companyId: number) {
    return { success: true, data: await this.billing.markPaidManually(companyId) };
  }

  /** POST /api/companies/:companyId/billing/revoke — cancel access (paywall returns) */
  @Post(':companyId/billing/revoke')
  @HttpCode(200)
  async revoke(@Param('companyId', ParseIntPipe) companyId: number) {
    return { success: true, data: await this.billing.revokeAccess(companyId) };
  }
}
