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

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

    @Get()
    findAll() {
        return this.tenantService.findAll();
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.tenantService.findById(id);
    }

    @Post()
    create(@Body() body: { name: string; description?: string }) {
        return this.tenantService.create(body.name, body.description);
    }

    @Put(':id')
    update(@Param('id') id: string, @Body() body: { name?: string; description?: string; isActive?: boolean }) {
        return this.tenantService.update(id, body);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.tenantService.remove(id);
    }

    @Get(':id/settings')
    getSettings(@Param('id') id: string) {
        return this.tenantService.getSettings(id);
    }

    @Put(':id/settings')
    updateSettings(@Param('id') id: string, @Body() body: any) {
        return this.tenantService.updateSettings(id, body);
    }

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

    @Post(':id/members')
    addMember(
        @Param('id') id: string,
        @Body() body: { userId: string; role?: string },
    ) {
        return this.tenantService.addMember(id, body.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);
    }
}
