import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { BetterChatService } from './betterchat.service';
import { CreateBetterChatConfigDto } from './dto/create-betterchat-config.dto';
import { UpdateBetterChatConfigDto } from './dto/update-betterchat-config.dto';
import { ImportBetterChatConfigDto } from './dto/import-betterchat-config.dto';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { PermissionsGuard } from '../../common/guards/permissions.guard';

@ApiTags('betterchat')
@ApiBearerAuth()
@Controller('betterchat')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class BetterChatController {
  constructor(private readonly betterChatService: BetterChatService) {}

  @Get('configs')
  @RequirePermission('betterchat.view')
  @ApiOperation({ summary: 'List BetterChat configs (summaries)' })
  getConfigs(@CurrentTenant() licenseId: string) {
    return this.betterChatService.getConfigs(licenseId);
  }

  @Get('configs/:id')
  @RequirePermission('betterchat.view')
  @ApiOperation({ summary: 'Get full BetterChat config with data' })
  getConfig(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.betterChatService.getConfig(licenseId, id);
  }

  @Post('configs')
  @RequirePermission('betterchat.manage')
  @ApiOperation({ summary: 'Create BetterChat config' })
  createConfig(@CurrentTenant() licenseId: string, @Body() dto: CreateBetterChatConfigDto) {
    return this.betterChatService.createConfig(licenseId, dto);
  }

  @Put('configs/:id')
  @RequirePermission('betterchat.manage')
  @ApiOperation({ summary: 'Update BetterChat config' })
  updateConfig(
    @CurrentTenant() licenseId: string,
    @Param('id') id: string,
    @Body() dto: UpdateBetterChatConfigDto,
  ) {
    return this.betterChatService.updateConfig(licenseId, id, dto);
  }

  @Delete('configs/:id')
  @RequirePermission('betterchat.manage')
  @ApiOperation({ summary: 'Delete BetterChat config' })
  deleteConfig(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.betterChatService.deleteConfig(licenseId, id);
  }

  @Post('configs/:id/apply')
  @RequirePermission('betterchat.manage')
  @ApiOperation({ summary: 'Deploy BetterChat config to server' })
  applyToServer(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.betterChatService.applyToServer(licenseId, id);
  }

  @Post('import-from-server')
  @RequirePermission('betterchat.manage')
  @ApiOperation({ summary: 'Import BetterChat.json from server via NATS' })
  importFromServer(@CurrentTenant() licenseId: string, @Body() dto: ImportBetterChatConfigDto) {
    return this.betterChatService.importFromServer(licenseId, dto.config_name, dto.description);
  }
}
