import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { EnterpriseService } from './enterprise.service';

@ApiTags('企业版')
@Controller('enterprise')
export class EnterpriseController {
  constructor(private enterpriseService: EnterpriseService) {}

  @Post('organizations')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async create(@Req() req: any, @Body() body: { name: string; description?: string; contactName?: string; contactPhone?: string }) {
    return this.enterpriseService.createOrganization(req.user.userId, body);
  }

  @Get('organizations')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async list(@Query('page') page?: string, @Query('pageSize') pageSize?: string) {
    return this.enterpriseService.listOrganizations(
      page ? parseInt(page) : 1,
      pageSize ? parseInt(pageSize) : 20,
    );
  }

  @Get('organizations/:id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async get(@Param('id') id: string) {
    return this.enterpriseService.getOrganization(parseInt(id));
  }

  @Put('organizations/:id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async update(@Param('id') id: string, @Body() body: { name?: string; description?: string; contactName?: string; contactPhone?: string }) {
    return this.enterpriseService.updateOrganization(parseInt(id), body);
  }

  @Post('organizations/:id/members')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async addMember(@Req() req: any, @Param('id') id: string, @Body() body: { userId: number; role?: string }) {
    return this.enterpriseService.addMember(parseInt(id), req.user.userId, body.userId, body.role);
  }

  @Delete('organizations/:orgId/members/:userId')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async removeMember(@Req() req: any, @Param('orgId') orgId: string, @Param('userId') userId: string) {
    return this.enterpriseService.removeMember(parseInt(orgId), parseInt(userId), req.user.userId);
  }

  @Post('organizations/:id/assignments')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async assignCourse(@Req() req: any, @Param('id') id: string, @Body() body: { courseId: number; deadline?: string }) {
    return this.enterpriseService.assignCourse(parseInt(id), body.courseId, req.user.userId, body.deadline);
  }

  @Delete('organizations/:orgId/assignments/:courseId')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async removeAssignment(@Param('orgId') orgId: string, @Param('courseId') courseId: string) {
    return this.enterpriseService.removeAssignment(parseInt(orgId), parseInt(courseId));
  }

  @Get('organizations/:id/progress')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async getProgress(@Param('id') id: string) {
    return this.enterpriseService.getOrgProgress(parseInt(id));
  }

  @Get('organizations/:id/report')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async getReport(@Param('id') id: string) {
    return this.enterpriseService.getOrganizationReport(parseInt(id));
  }

  @Get('my')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async myOrganizations(@Req() req: any) {
    return this.enterpriseService.getMyOrganizations(req.user.userId);
  }
}
