import { Controller, Get, Post, Put, Body, UseGuards, Param, Delete, HttpCode, Patch, ForbiddenException, Query } from '@nestjs/common';
import { SuperAdminService } from './super-admin.service';
import { TenantService } from '../tenant/tenant.service';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { UserRole } from '../user/user-role.enum';
import { I18nService } from '../i18n/i18n.service';

@Controller('v1/tenants')
@UseGuards(CombinedAuthGuard, RolesGuard)
@Roles(UserRole.SUPER_ADMIN)
export class SuperAdminController {
    constructor(
        private readonly superAdminService: SuperAdminService,
        private readonly tenantService: TenantService,
        private readonly i18nService: I18nService,
    ) { }

    @Get()
    async getTenants(
        @Query('page') page?: string,
        @Query('limit') limit?: string,
    ) {
        return this.superAdminService.getAllTenants(
            page ? parseInt(page) : undefined,
            limit ? parseInt(limit) : undefined
        );
    }

    @Post()
    async createTenant(@Body() body: { name: string; domain?: string; adminUserId?: string; parentId?: string }) {
        return this.superAdminService.createTenant(body.name, body.domain, body.adminUserId, body.parentId);
    }

    @Put(':tenantId/admin')
    async bindTenantAdmin(
        @Param('tenantId') tenantId: string,
        @Body() body: { userId: string }
    ) {
        return this.superAdminService.assignUserToTenant(body.userId, tenantId);
    }

    @Post(':tenantId/admin/new')
    async createTenantAdmin(
        @Param('tenantId') tenantId: string,
        @Body() body: { username: string; password?: string }
    ) {
        return this.superAdminService.createTenantAdmin(tenantId, body.username, body.password);
    }

    @Put(':tenantId')
    async updateTenant(
        @Param('tenantId') tenantId: string,
        @Body() body: { name?: string; domain?: string; parentId?: string }
    ) {
        return this.superAdminService.updateTenant(tenantId, body);
    }

    @Delete(':tenantId')
    async deleteTenant(@Param('tenantId') tenantId: string) {
        return this.superAdminService.deleteTenant(tenantId);
    }

    // --- Member Management ---

    @Get(':tenantId/members')
    async getMembers(
        @Param('tenantId') tenantId: 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(tenantId, p, l);
    }

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

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

    @Patch(':tenantId/members/:userId')
    async updateMemberRole(
        @Param('tenantId') tenantId: string,
        @Param('userId') userId: string,
        @Body() body: { role: string },
    ) {
        if (body.role !== UserRole.USER && body.role !== UserRole.TENANT_ADMIN) {
            throw new ForbiddenException(this.i18nService.getErrorMessage('invalidMemberRole'));
        }
        return this.tenantService.updateMemberRole(tenantId, userId, body.role);
    }

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