import {
    Body,
    Controller,
    Delete,
    ForbiddenException,
    Get,
    Param,
    Patch,
    Post,
    Put,
    Request,
    UseGuards,
    HttpCode,
    Query,
} from '@nestjs/common';
import { TenantService } from './tenant.service';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { SuperAdminGuard } from '../auth/super-admin.guard';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';

@ApiTags('Tenants')
@Controller('tenants')
@UseGuards(CombinedAuthGuard, SuperAdminGuard)
export class TenantController {
    constructor(private readonly tenantService: TenantService) { }

    @Get()
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '获取租户列表', description: '超级管理员专用' })
    @ApiQuery({ name: 'page', required: false })
    @ApiQuery({ name: 'limit', required: false })
    @ApiResponse({ status: 200, description: '返回租户列表' })
    findAll(
        @Query('page') page?: string,
        @Query('limit') limit?: string,
    ) {
        const p = page ? parseInt(page) : undefined;
        const l = limit ? parseInt(limit) : undefined;
        return this.tenantService.findAll(p, l);
    }

    @Get(':id')
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '获取单个租户信息' })
    @ApiResponse({ status: 200, description: '返回租户详情' })
    findOne(@Param('id') id: string) {
        return this.tenantService.findById(id);
    }

    @Post()
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '创建租户', description: '超级管理员专用' })
    @ApiResponse({ status: 201, description: '创建成功' })
    create(@Body() body: { name: string; domain?: string; parentId?: string }) {
        return this.tenantService.create(body.name, body.domain, body.parentId);
    }

    @Put(':id')
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '更新租户' })
    @ApiResponse({ status: 200, description: '更新成功' })
    update(@Param('id') id: string, @Body() body: { name?: string; domain?: string; parentId?: string; isActive?: boolean }) {
        return this.tenantService.update(id, body);
    }

    @Delete(':id')
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '删除租户' })
    @ApiResponse({ status: 200, description: '删除成功' })
    remove(@Param('id') id: string) {
        return this.tenantService.remove(id);
    }

    @Get(':id/settings')
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '获取租户设置' })
    @ApiResponse({ status: 200, description: '返回租户设置' })
    getSettings(@Param('id') id: string) {
        return this.tenantService.getSettings(id);
    }

    @Put(':id/settings')
    @ApiBearerAuth('JWT')
    @ApiOperation({ summary: '更新租户设置' })
    @ApiResponse({ status: 200, description: '更新成功' })
    updateSettings(@Param('id') id: string, @Body() body: any) {
        return this.tenantService.updateSettings(id, body);
    }

    @Get(':id/members')
    getMembers(
        @Param('id') id: string,
        @Query('page') page?: string,
        @Query('limit') limit?: string,
    ) {
        const p = page ? parseInt(page) : undefined;
        const l = limit ? parseInt(limit) : undefined;
        return this.tenantService.getMembers(id, p, l);
    }

    @Post(':id/members')
    addMember(
        @Param('id') id: string,
        @Body() body: { userId: string; role?: string },
    ) {
        return this.tenantService.addMember(id, body.userId, body.role);
    }

    @Patch(':id/members/:userId')
    async updateMemberRole(
        @Param('id') id: string,
        @Param('userId') userId: string,
        @Body() body: { role: string },
    ) {
        return this.tenantService.updateMemberRole(id, userId, body.role);
    }

    @Delete(':id/members/:userId')
    @HttpCode(204)
    async removeMember(
        @Param('id') id: string,
        @Param('userId') userId: string,
    ) {
        await this.tenantService.removeMember(id, userId);
    }

    @Get(':id/members/ids')
    getMemberIds(
        @Param('id') id: string,
    ) {
        return this.tenantService.getMemberIds(id);
    }
}
