import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { TimetableService } from './timetable.service';
import { JwtAuthGuard, RolesGuard } from '../auth/guards';
import { Roles } from '../auth/guards/roles.guard';
import { CreateTimetableEntryDto, UpdateTimetableEntryDto } from './dto';

@Controller('timetable')
@UseGuards(JwtAuthGuard)
export class TimetableController {
  constructor(private readonly timetableService: TimetableService) {}

  // GET /timetable?weekNumber=1&year=2025 - Stundenplan für eine Woche abrufen
  @Get()
  async getTimetable(
    @Query('weekNumber') weekNumber?: string,
    @Query('year') year?: string,
  ) {
    const week = weekNumber ? parseInt(weekNumber, 10) : undefined;
    const yr = year ? parseInt(year, 10) : undefined;
    return this.timetableService.getTimetable(week, yr);
  }

  // POST /timetable - Neuen Stundenplan-Eintrag erstellen (nur Lehrer)
  @Post()
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async createEntry(@Body() createDto: CreateTimetableEntryDto) {
    return this.timetableService.createEntry(createDto);
  }

  // PUT /timetable/:id - Stundenplan-Eintrag aktualisieren (nur Lehrer)
  @Put(':id')
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async updateEntry(
    @Param('id', ParseIntPipe) entryId: number,
    @Body() updateDto: UpdateTimetableEntryDto,
  ) {
    return this.timetableService.updateEntry(entryId, updateDto);
  }

  // DELETE /timetable/:id - Stundenplan-Eintrag löschen (nur Lehrer)
  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async deleteEntry(@Param('id', ParseIntPipe) entryId: number) {
    return this.timetableService.deleteEntry(entryId);
  }
}
