import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { FurnaceSplitterService } from './furnacesplitter.service';
import { CreateFurnaceSplitterConfigDto } from './dto/create-furnacesplitter-config.dto';
import { UpdateFurnaceSplitterConfigDto } from './dto/update-furnacesplitter-config.dto';
import { ImportFurnaceSplitterConfigDto } from './dto/import-furnacesplitter-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('furnacesplitter')
@ApiBearerAuth()
@Controller('furnacesplitter')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class FurnaceSplitterController {
  constructor(private readonly furnaceSplitterService: FurnaceSplitterService) {}

  @Get('configs')
  @RequirePermission('furnacesplitter.view')
  @ApiOperation({ summary: 'List furnace splitter configs (summaries)' })
  getConfigs(@CurrentTenant() licenseId: string) {
    return this.furnaceSplitterService.getConfigs(licenseId);
  }

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

  @Post('configs')
  @RequirePermission('furnacesplitter.manage')
  @ApiOperation({ summary: 'Create furnace splitter config' })
  createConfig(@CurrentTenant() licenseId: string, @Body() dto: CreateFurnaceSplitterConfigDto) {
    return this.furnaceSplitterService.createConfig(licenseId, dto);
  }

  @Put('configs/:id')
  @RequirePermission('furnacesplitter.manage')
  @ApiOperation({ summary: 'Update furnace splitter config' })
  updateConfig(
    @CurrentTenant() licenseId: string,
    @Param('id') id: string,
    @Body() dto: UpdateFurnaceSplitterConfigDto,
  ) {
    return this.furnaceSplitterService.updateConfig(licenseId, id, dto);
  }

  @Delete('configs/:id')
  @RequirePermission('furnacesplitter.manage')
  @ApiOperation({ summary: 'Delete furnace splitter config' })
  deleteConfig(@CurrentTenant() licenseId: string, @Param('id') id: string) {
    return this.furnaceSplitterService.deleteConfig(licenseId, id);
  }

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

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