import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { ContentPieceService } from '../services/content-piece.service';
import { CreateContentPieceDto, UpdateContentPieceDto } from '../dto';
import { ContentType, ContentStatus } from '../entities/content-piece.entity';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { TenantMemberGuard } from '@/common/guards/tenant-member.guard';
import { CurrentTenant } from '@/common/decorators/current-tenant.decorator';
import { PaginationDto } from '@/shared/dto/pagination.dto';

@ApiTags('Content Pieces')
@Controller('content-pieces')
@UseGuards(JwtAuthGuard, TenantMemberGuard)
@ApiBearerAuth('JWT-auth')
export class ContentPieceController {
  constructor(private readonly contentPieceService: ContentPieceService) {}

  @Get()
  @ApiOperation({ summary: 'Get all content pieces with pagination' })
  @ApiQuery({ name: 'projectId', required: false })
  @ApiQuery({ name: 'type', enum: ContentType, required: false })
  @ApiQuery({ name: 'status', enum: ContentStatus, required: false })
  @ApiQuery({ name: 'search', required: false })
  @ApiResponse({ status: 200, description: 'List of content pieces' })
  async findAll(
    @CurrentTenant() tenantId: string,
    @Query() pagination: PaginationDto,
    @Query('projectId') projectId?: string,
    @Query('type') type?: ContentType,
    @Query('status') status?: ContentStatus,
    @Query('search') search?: string,
  ) {
    return this.contentPieceService.findAllPaginated(tenantId, {
      ...pagination,
      projectId,
      type,
      status,
      search,
    });
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get content piece by ID' })
  @ApiResponse({ status: 200, description: 'Content piece found' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async findOne(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.contentPieceService.findById(tenantId, id);
  }

  @Get('project/:projectId')
  @ApiOperation({ summary: 'Get content pieces by project' })
  @ApiResponse({ status: 200, description: 'List of project content pieces' })
  async findByProject(
    @CurrentTenant() tenantId: string,
    @Param('projectId', ParseUUIDPipe) projectId: string,
  ) {
    return this.contentPieceService.findByProject(tenantId, projectId);
  }

  @Post()
  @ApiOperation({ summary: 'Create new content piece' })
  @ApiResponse({ status: 201, description: 'Content piece created' })
  @ApiResponse({ status: 400, description: 'Bad request' })
  async create(
    @CurrentTenant() tenantId: string,
    @Body() dto: CreateContentPieceDto,
  ) {
    return this.contentPieceService.create(tenantId, dto);
  }

  @Post(':id/duplicate')
  @ApiOperation({ summary: 'Duplicate content piece' })
  @ApiResponse({ status: 201, description: 'Content piece duplicated' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async duplicate(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.contentPieceService.duplicate(tenantId, id);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update content piece' })
  @ApiResponse({ status: 200, description: 'Content piece updated' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async update(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateContentPieceDto,
  ) {
    return this.contentPieceService.update(tenantId, id, dto);
  }

  @Put(':id/status')
  @ApiOperation({ summary: 'Update content piece status' })
  @ApiResponse({ status: 200, description: 'Content piece status updated' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async updateStatus(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('status') status: ContentStatus,
  ) {
    return this.contentPieceService.updateStatus(tenantId, id, status);
  }

  @Put(':id/assign')
  @ApiOperation({ summary: 'Assign content piece to user' })
  @ApiResponse({ status: 200, description: 'Content piece assigned' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async assign(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('userId') userId: string | null,
  ) {
    return this.contentPieceService.assignTo(tenantId, id, userId);
  }

  @Put(':id/schedule')
  @ApiOperation({ summary: 'Schedule content piece' })
  @ApiResponse({ status: 200, description: 'Content piece scheduled' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async schedule(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('scheduledAt') scheduledAt: string,
  ) {
    return this.contentPieceService.schedule(tenantId, id, new Date(scheduledAt));
  }

  @Put(':id/publish')
  @ApiOperation({ summary: 'Mark content piece as published' })
  @ApiResponse({ status: 200, description: 'Content piece published' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async publish(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('publishedUrl') publishedUrl?: string,
  ) {
    return this.contentPieceService.publish(tenantId, id, publishedUrl);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete content piece (soft delete)' })
  @ApiResponse({ status: 204, description: 'Content piece deleted' })
  @ApiResponse({ status: 404, description: 'Content piece not found' })
  async delete(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.contentPieceService.softDelete(tenantId, id);
  }
}
