import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { SubjectsService } from './subjects.service';
import { JwtAuthGuard, RolesGuard } from '../auth/guards';
import { Roles } from '../auth/guards/roles.guard';
import { CreateSubjectDto, UpdateSubjectDto } from './dto';

@Controller('subjects')
@UseGuards(JwtAuthGuard)
export class SubjectsController {
  constructor(private readonly subjectsService: SubjectsService) {}

  // GET /subjects - Alle Fächer abrufen
  @Get()
  async findAll() {
    return this.subjectsService.findAll();
  }

  // GET /subjects/:id - Ein Fach nach ID abrufen
  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return this.subjectsService.findOne(id);
  }

  // POST /subjects - Neues Fach erstellen (nur Lehrer)
  @Post()
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async create(@Body() createDto: CreateSubjectDto) {
    return this.subjectsService.create(createDto);
  }

  // PUT /subjects/:id - Fach aktualisieren (nur Lehrer)
  @Put(':id')
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateDto: UpdateSubjectDto,
  ) {
    return this.subjectsService.update(id, updateDto);
  }

  // DELETE /subjects/:id - Fach löschen (nur Lehrer)
  @Delete(':id')
  @UseGuards(RolesGuard)
  @Roles(['Lehrer'])
  async delete(@Param('id', ParseIntPipe) id: number) {
    return this.subjectsService.delete(id);
  }
}
