import {
  Controller,
  Get,
  Post,
  Put,
  Patch,
  Delete,
  Body,
  Param,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
import { SchedulesService } from './schedules.service';
import { CreateTaskDto } from './dto/create-task.dto';
import { UpdateTaskDto } from './dto/update-task.dto';

@ApiTags('schedules')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('schedules')
export class SchedulesController {
  constructor(private readonly schedulesService: SchedulesService) {}

  @Get('tasks')
  @RequirePermission('schedules.view')
  @ApiOperation({
    summary: 'Get all scheduled tasks',
    description: 'Returns all scheduled tasks for this license',
  })
  @ApiResponse({
    status: 200,
    description: 'Tasks retrieved successfully',
  })
  async getTasks(@CurrentTenant() licenseId: string) {
    return await this.schedulesService.getTasks(licenseId);
  }

  @Post('tasks')
  @RequirePermission('schedules.manage')
  @ApiOperation({
    summary: 'Create a scheduled task',
    description: 'Create a new scheduled task (restart, announcement, command, or plugin reload)',
  })
  @ApiResponse({
    status: 201,
    description: 'Task created successfully',
  })
  @ApiResponse({
    status: 400,
    description: 'Invalid cron expression',
  })
  async createTask(
    @CurrentTenant() licenseId: string,
    @Body() dto: CreateTaskDto,
  ) {
    return await this.schedulesService.createTask(licenseId, dto);
  }

  @Put('tasks/:id')
  @RequirePermission('schedules.manage')
  @ApiOperation({
    summary: 'Update a scheduled task',
    description: 'Update task configuration, schedule, or settings',
  })
  @ApiResponse({
    status: 200,
    description: 'Task updated successfully',
  })
  @ApiResponse({
    status: 404,
    description: 'Task not found',
  })
  async updateTask(
    @CurrentTenant() licenseId: string,
    @Param('id') taskId: string,
    @Body() dto: UpdateTaskDto,
  ) {
    return await this.schedulesService.updateTask(licenseId, taskId, dto);
  }

  @Patch('tasks/:id/toggle')
  @RequirePermission('schedules.manage')
  @ApiOperation({
    summary: 'Toggle a scheduled task',
    description: 'Enable or disable a scheduled task without deleting it',
  })
  @ApiResponse({
    status: 200,
    description: 'Task toggled successfully',
  })
  @ApiResponse({
    status: 404,
    description: 'Task not found',
  })
  async toggleTask(
    @CurrentTenant() licenseId: string,
    @Param('id', ParseUUIDPipe) taskId: string,
    @Body('enabled') enabled: boolean,
  ) {
    return await this.schedulesService.toggleTask(licenseId, taskId, enabled);
  }

  @Delete('tasks/:id')
  @RequirePermission('schedules.manage')
  @ApiOperation({
    summary: 'Delete a scheduled task',
    description: 'Remove a scheduled task and unregister from scheduler',
  })
  @ApiResponse({
    status: 200,
    description: 'Task deleted successfully',
  })
  @ApiResponse({
    status: 404,
    description: 'Task not found',
  })
  async deleteTask(
    @CurrentTenant() licenseId: string,
    @Param('id', ParseUUIDPipe) taskId: string,
  ) {
    return await this.schedulesService.deleteTask(licenseId, taskId);
  }
}
