import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseIntPipe,
  UseGuards,
  Request,
} from '@nestjs/common';
import { CoursePackageService } from './course-package.service';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

@Controller('admin/packages')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
export class CoursePackageController {
  constructor(private readonly packageService: CoursePackageService) {}

  // ==================== 套餐管理 ====================

  @Get()
  async findAll(
    @Query('status') status?: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    return this.packageService.findAllPackages({
      status,
      page: page ? parseInt(page, 10) : 1,
      pageSize: pageSize ? parseInt(pageSize, 10) : 20,
    });
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return this.packageService.findOnePackage(id);
  }

  @Post()
  async create(
    @Body()
    body: {
      name: string;
      description?: string;
      price: number;
      discountPrice?: number;
      discountType?: string;
      gradeLevels: string[];
    },
  ) {
    return this.packageService.createPackage(body);
  }

  @Put(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body()
    body: {
      name?: string;
      description?: string;
      price?: number;
      discountPrice?: number;
      discountType?: string;
      gradeLevels?: string[];
    },
  ) {
    return this.packageService.updatePackage(id, body);
  }

  @Delete(':id')
  async remove(@Param('id', ParseIntPipe) id: number) {
    return this.packageService.deletePackage(id);
  }

  // ==================== 套餐课程管理 ====================

  @Put(':id/courses')
  async setCourses(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { courses: { courseId: number; gradeLevel: string; sortOrder?: number }[] },
  ) {
    return this.packageService.setPackageCourses(id, body.courses);
  }

  @Post(':id/courses')
  async addCourse(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { courseId: number; gradeLevel: string; sortOrder?: number },
  ) {
    return this.packageService.addCourseToPackage(
      id,
      body.courseId,
      body.gradeLevel,
      body.sortOrder,
    );
  }

  @Delete(':id/courses/:courseId')
  async removeCourse(
    @Param('id', ParseIntPipe) id: number,
    @Param('courseId', ParseIntPipe) courseId: number,
  ) {
    return this.packageService.removeCourseFromPackage(id, courseId);
  }

  // ==================== 套餐状态管理 ====================

  @Post(':id/submit')
  async submit(@Param('id', ParseIntPipe) id: number, @Request() req: any) {
    return this.packageService.submitPackage(id, req.user.id);
  }

  @Post(':id/review')
  async review(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { approved: boolean; comment?: string },
    @Request() req: any,
  ) {
    return this.packageService.reviewPackage(id, req.user.id, body.approved, body.comment);
  }

  @Post(':id/publish')
  async publish(@Param('id', ParseIntPipe) id: number) {
    return this.packageService.publishPackage(id);
  }

  @Post(':id/offline')
  async offline(@Param('id', ParseIntPipe) id: number) {
    return this.packageService.offlinePackage(id);
  }
}

// 学校端套餐控制器
@Controller('school/packages')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class SchoolPackageController {
  constructor(private readonly packageService: CoursePackageService) {}

  @Get()
  async findTenantPackages(@Request() req: any) {
    return this.packageService.findTenantPackages(req.user.tenantId);
  }

  @Post(':id/renew')
  async renewPackage(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { endDate: string; pricePaid?: number },
    @Request() req: any,
  ) {
    return this.packageService.renewTenantPackage(
      req.user.tenantId,
      id,
      body.endDate,
      body.pricePaid,
    );
  }
}
