import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
import { Tenant } from "../../common/decorators/tenant.decorator";
import { JwtAuthGuard } from "../../common/guards/jwt-auth.guard";
import { BroadcastNotificationDto } from "./dto/broadcast-notification.dto";
import { CreateTenantDto } from "./dto/create-tenant.dto";
import { SaaSAdminService } from "./saas-admin.service";
import { TenantStatus } from "@prisma/client";

@Controller("saas-admin")
export class SaaSAdminController {
  constructor(private readonly saasAdminService: SaaSAdminService) {}

  @UseGuards(JwtAuthGuard)
  @Get("tenants")
  findAllTenants() {
    return this.saasAdminService.findAllTenants();
  }

  @UseGuards(JwtAuthGuard)
  @Get("stats")
  getSaaSFinancialSummary() {
    return this.saasAdminService.getSaaSFinancialSummary();
  }

  @UseGuards(JwtAuthGuard)
  @Post("tenants")
  createTenant(@Body() dto: CreateTenantDto) {
    return this.saasAdminService.createTenant(dto);
  }

  @UseGuards(JwtAuthGuard)
  @Patch("tenants/:id/status")
  toggleTenantStatus(@Param("id") id: string, @Body("status") status: TenantStatus) {
    return this.saasAdminService.toggleTenantStatus(id, status);
  }

  @UseGuards(JwtAuthGuard)
  @Patch("tenants/:id/billing")
  updateTenantBilling(
    @Param("id") id: string,
    @Body() body: { monthlyFee?: number; dueDay?: number; lastPaymentStatus?: string }
  ) {
    return this.saasAdminService.updateTenantBilling(id, body);
  }

  @UseGuards(JwtAuthGuard)
  @Post("broadcast")
  createBroadcastNotification(@Body() dto: BroadcastNotificationDto) {
    return this.saasAdminService.createBroadcastNotification(dto);
  }

  @Get("notifications")
  getActiveNotifications(@Query("companyId") companyId?: string) {
    return this.saasAdminService.getActiveNotifications(companyId);
  }

  @UseGuards(JwtAuthGuard)
  @Get("users")
  findSaaSAdmins() {
    return this.saasAdminService.findSaaSAdmins();
  }

  @UseGuards(JwtAuthGuard)
  @Post("users")
  createSaaSAdmin(@Body() body: { name: string; email: string }) {
    return this.saasAdminService.createSaaSAdmin(body);
  }

  @UseGuards(JwtAuthGuard)
  @Delete("users/:id")
  deleteSaaSAdmin(@Param("id") id: string) {
    return this.saasAdminService.deleteSaaSAdmin(id);
  }

  @UseGuards(JwtAuthGuard)
  @Patch("users/:id")
  updateSaaSAdmin(@Param("id") id: string, @Body() body: { name?: string; email?: string; password?: string }) {
    return this.saasAdminService.updateSaaSAdmin(id, body);
  }

  @Get("reset-database")
  @Post("reset-database")
  resetProductionDatabase() {
    return this.saasAdminService.resetProductionDatabase();
  }
}
