import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Param,
  Query,
  Headers,
  HttpCode,
  HttpStatus,
  UnauthorizedException,
} from '@nestjs/common';
import {
  TenantManagementService,
  CreateTenantDto,
  UpdateTenantDto,
  CreateTenantAdminDto,
} from '../../application/services/tenant-management.service';

/**
 * 超级管理员 - 租户管理 API
 *
 * 所有接口需要在 Header 中传递:
 * - x-admin-id: 超级管理员ID
 *
 * 路由前缀: /super-admin/tenants
 */
@Controller('super-admin/tenants')
export class TenantController {
  constructor(private readonly tenantService: TenantManagementService) {}

  /**
   * 验证超级管理员权限
   */
  private async checkSuperAdmin(adminId: string | undefined) {
    if (!adminId) {
      throw new UnauthorizedException('需要提供管理员ID (x-admin-id header)');
    }
    return this.tenantService.verifySuperAdmin(adminId);
  }

  /**
   * 获取全局统计
   * GET /super-admin/tenants/stats
   */
  @Get('stats')
  async getGlobalStats(@Headers('x-admin-id') adminId: string) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.getGlobalStats();
  }

  /**
   * 获取租户列表
   * GET /super-admin/tenants
   */
  @Get()
  async listTenants(
    @Headers('x-admin-id') adminId: string,
    @Query('status') status?: string,
    @Query('plan') plan?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.listTenants({
      status,
      plan,
      page: page ? parseInt(page) : 1,
      limit: limit ? parseInt(limit) : 20,
    });
  }

  /**
   * 获取租户详情
   * GET /super-admin/tenants/:id
   */
  @Get(':id')
  async getTenant(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.getTenant(tenantId);
  }

  /**
   * 获取租户统计
   * GET /super-admin/tenants/:id/stats
   */
  @Get(':id/stats')
  async getTenantStats(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.getTenantStats(tenantId);
  }

  /**
   * 创建租户
   * POST /super-admin/tenants
   */
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createTenant(
    @Headers('x-admin-id') adminId: string,
    @Body() dto: CreateTenantDto,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.createTenant(dto);
  }

  /**
   * 更新租户
   * PUT /super-admin/tenants/:id
   */
  @Put(':id')
  async updateTenant(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
    @Body() dto: UpdateTenantDto,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.updateTenant(tenantId, dto);
  }

  /**
   * 暂停租户
   * POST /super-admin/tenants/:id/suspend
   */
  @Post(':id/suspend')
  async suspendTenant(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
    @Body('reason') reason?: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.suspendTenant(tenantId, reason);
  }

  /**
   * 激活租户
   * POST /super-admin/tenants/:id/activate
   */
  @Post(':id/activate')
  async activateTenant(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.activateTenant(tenantId);
  }

  /**
   * 归档租户
   * POST /super-admin/tenants/:id/archive
   */
  @Post(':id/archive')
  async archiveTenant(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.archiveTenant(tenantId);
  }

  /**
   * 获取租户管理员列表
   * GET /super-admin/tenants/:id/admins
   */
  @Get(':id/admins')
  async listTenantAdmins(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
  ) {
    await this.checkSuperAdmin(adminId);
    return this.tenantService.listTenantAdmins(tenantId);
  }

  /**
   * 为租户创建管理员
   * POST /super-admin/tenants/:id/admins
   */
  @Post(':id/admins')
  @HttpCode(HttpStatus.CREATED)
  async createTenantAdmin(
    @Headers('x-admin-id') adminId: string,
    @Param('id') tenantId: string,
    @Body() dto: CreateTenantAdminDto,
  ) {
    await this.checkSuperAdmin(adminId);
    const admin = await this.tenantService.createTenantAdmin(tenantId, dto);
    // 不返回密码哈希
    const { passwordHash, ...result } = admin;
    return result;
  }
}
