import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { NewsService } from './news.service';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

@ApiTags('News')
@Controller()
export class NewsController {
  constructor(private newsService: NewsService) {}

  @Get('news')
  findAll() {
    return this.newsService.findAll();
  }

  @Get('news/latest')
  findLatest(@Query('limit') limit: string) {
    return this.newsService.findLatest(limit ? +limit : 5);
  }

  @Get('news/:slug')
  findBySlug(@Param('slug') slug: string) {
    return this.newsService.findBySlug(slug);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('SUPER_ADMIN', 'ADMIN')
  @Get('admin/news')
  adminFindAll() {
    return this.newsService.adminFindAll();
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('SUPER_ADMIN', 'ADMIN')
  @Post('admin/news')
  create(@Body() data: { title: string; content: string; excerpt?: string; featuredImage?: string; status?: any; publishedAt?: string }) {
    return this.newsService.create(data);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('SUPER_ADMIN', 'ADMIN')
  @Put('admin/news/:id')
  update(@Param('id') id: string, @Body() data: { title?: string; content?: string; excerpt?: string; featuredImage?: string; status?: any; publishedAt?: string }) {
    return this.newsService.update(+id, data);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('SUPER_ADMIN', 'ADMIN')
  @Delete('admin/news/:id')
  remove(@Param('id') id: string) {
    return this.newsService.remove(+id);
  }
}
