import {
  Controller,
  Post,
  Get,
  Patch,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { FeedbackService } from './feedback.service';
import { CreateFeedbackDto } from './dto/create-feedback.dto';
import {
  Feedback,
  FeedbackStatus,
  FeedbackType,
  FeedbackPriority,
} from './feedback.entity';

@Controller('feedback')
@UseGuards(JwtAuthGuard)
export class FeedbackController {
  constructor(private readonly feedbackService: FeedbackService) {}

  /**
   * Submit new feedback
   * POST /api/v1/feedback
   */
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createFeedback(
    @Request() req,
    @Body() dto: CreateFeedbackDto,
  ): Promise<Feedback> {
    return this.feedbackService.createFeedback(req.user.id, dto);
  }

  /**
   * Get user's feedback history
   * GET /api/v1/feedback/my
   */
  @Get('my')
  async getMyFeedback(
    @Request() req,
    @Query('type') type?: FeedbackType,
    @Query('status') status?: FeedbackStatus,
  ): Promise<Feedback[]> {
    return this.feedbackService.getUserFeedback(req.user.id, { type, status });
  }

  /**
   * Get specific feedback
   * GET /api/v1/feedback/:id
   */
  @Get(':id')
  async getFeedback(@Param('id') id: string): Promise<Feedback> {
    return this.feedbackService.getFeedback(id);
  }

  /**
   * Upvote a feature request
   * POST /api/v1/feedback/:id/upvote
   */
  @Post(':id/upvote')
  async upvoteFeedback(
    @Request() req,
    @Param('id') id: string,
  ): Promise<Feedback> {
    return this.feedbackService.upvoteFeedback(id, req.user.id);
  }

  /**
   * Get trending feature requests
   * GET /api/v1/feedback/trending/features
   */
  @Get('trending/features')
  async getTrendingFeatures(
    @Query('limit') limit: number = 10,
  ): Promise<Feedback[]> {
    return this.feedbackService.getTrendingFeatureRequests(limit);
  }

  // ============ Admin Endpoints ============
  // TODO: Add admin guard

  /**
   * Get all feedback (admin only)
   * GET /api/v1/feedback/admin/all
   */
  @Get('admin/all')
  async getAllFeedback(
    @Query('type') type?: FeedbackType,
    @Query('status') status?: FeedbackStatus,
    @Query('priority') priority?: FeedbackPriority,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 50,
  ): Promise<{ feedback: Feedback[]; total: number }> {
    return this.feedbackService.getAllFeedback(
      { type, status, priority },
      page,
      limit,
    );
  }

  /**
   * Update feedback status (admin only)
   * PATCH /api/v1/feedback/admin/:id/status
   */
  @Patch('admin/:id/status')
  async updateStatus(
    @Request() req,
    @Param('id') id: string,
    @Body('status') status: FeedbackStatus,
    @Body('resolution') resolution?: string,
  ): Promise<Feedback> {
    return this.feedbackService.updateStatus(
      id,
      status,
      req.user.id,
      resolution,
    );
  }

  /**
   * Assign feedback (admin only)
   * PATCH /api/v1/feedback/admin/:id/assign
   */
  @Patch('admin/:id/assign')
  async assignFeedback(
    @Param('id') id: string,
    @Body('assignedTo') assignedTo: string,
  ): Promise<Feedback> {
    return this.feedbackService.assignFeedback(id, assignedTo);
  }

  /**
   * Add internal notes (admin only)
   * PATCH /api/v1/feedback/admin/:id/notes
   */
  @Patch('admin/:id/notes')
  async addNotes(
    @Param('id') id: string,
    @Body('notes') notes: string,
  ): Promise<Feedback> {
    return this.feedbackService.addInternalNotes(id, notes);
  }

  /**
   * Get feedback statistics (admin only)
   * GET /api/v1/feedback/admin/stats
   */
  @Get('admin/stats')
  async getStats() {
    return this.feedbackService.getFeedbackStats();
  }
}
