import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  HttpStatus,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { SeriesService } from './series.service';
import { CreateSeriesDto } from './dto/create-series.dto';
import { CreateSeasonDto } from './dto/create-season.dto';
import { CreateEpisodeDto } from './dto/create-episode.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { UserRole } from '../common/enums';

@ApiTags('Series')
@Controller('series')
export class SeriesController {
  constructor(private readonly seriesService: SeriesService) {}

  @Get()
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all series' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async findAll(
    @Query('page') page: string = '1',
    @Query('limit') limit: string = '20',
  ) {
    const result = await this.seriesService.findAll({
      page: Math.max(1, parseInt(page, 10) || 1),
      limit: Math.min(100, Math.max(1, parseInt(limit, 10) || 20)),
    });

    return {
      ...result,
      message: 'Series retrieved successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get series by ID (includes seasons and episodes)' })
  async findOne(@Param('id', ParseUUIDPipe) id: string) {
    const series = await this.seriesService.findById(id);
    return {
      data: series,
      message: 'Series retrieved successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Post()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create series (admin only)' })
  async create(@Body() createSeriesDto: CreateSeriesDto) {
    const series = await this.seriesService.create(createSeriesDto);
    return {
      data: series,
      message: 'Series created successfully',
      statusCode: HttpStatus.CREATED,
    };
  }

  @Patch(':id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update series (admin only)' })
  async update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() updateSeriesDto: Partial<CreateSeriesDto>,
  ) {
    const series = await this.seriesService.update(id, updateSeriesDto);
    return {
      data: series,
      message: 'Series updated successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Delete series (admin only)' })
  async remove(@Param('id', ParseUUIDPipe) id: string) {
    await this.seriesService.remove(id);
    return {
      data: null,
      message: 'Series deleted successfully',
      statusCode: HttpStatus.OK,
    };
  }

  @Post(':id/seasons')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Add season to series (admin only)' })
  async createSeason(
    @Param('id', ParseUUIDPipe) seriesId: string,
    @Body() createSeasonDto: CreateSeasonDto,
  ) {
    const season = await this.seriesService.createSeason(seriesId, createSeasonDto);
    return {
      data: season,
      message: 'Season created successfully',
      statusCode: HttpStatus.CREATED,
    };
  }

  @Post(':seriesId/seasons/:seasonId/episodes')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Add episode to season (admin only)' })
  async createEpisode(
    @Param('seriesId', ParseUUIDPipe) seriesId: string,
    @Param('seasonId', ParseUUIDPipe) seasonId: string,
    @Body() createEpisodeDto: CreateEpisodeDto,
  ) {
    const episode = await this.seriesService.createEpisode(
      seriesId,
      seasonId,
      createEpisodeDto,
    );
    return {
      data: episode,
      message: 'Episode created successfully',
      statusCode: HttpStatus.CREATED,
    };
  }
}
