import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { TimedExecuteService } from './timedexecute.service';
import { CreateTimedExecuteConfigDto } from './dto/create-timedexecute-config.dto';
import { UpdateTimedExecuteConfigDto } from './dto/update-timedexecute-config.dto';
import { ImportTimedExecuteConfigDto } from './dto/import-timedexecute-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('timedexecute')
@ApiBearerAuth()
@Controller('timedexecute')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class TimedExecuteController {
  constructor(private readonly timedExecuteService: TimedExecuteService) {}

  @Get('configs')
  @RequirePermission('timedexecute.view')
  @ApiOperation({ summary: 'List TimedExecute configs (summaries)' })
  getConfigs(@CurrentTenant() licenseId: string) {
    return this.timedExecuteService.getConfigs(licenseId);
  }

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

  @Post('configs')
  @RequirePermission('timedexecute.manage')
  @ApiOperation({ summary: 'Create TimedExecute config' })
  createConfig(@CurrentTenant() licenseId: string, @Body() dto: CreateTimedExecuteConfigDto) {
    return this.timedExecuteService.createConfig(licenseId, dto);
  }

  @Put('configs/:id')
  @RequirePermission('timedexecute.manage')
  @ApiOperation({ summary: 'Update TimedExecute config' })
  updateConfig(
    @CurrentTenant() licenseId: string,
    @Param('id') id: string,
    @Body() dto: UpdateTimedExecuteConfigDto,
  ) {
    return this.timedExecuteService.updateConfig(licenseId, id, dto);
  }

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

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

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