import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Put,
  UseInterceptors,
  UploadedFile,
  ParseIntPipe,
  UseGuards,
  BadRequestException,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AdminService } from './admin.service';
import { UpdateDocumentDto } from '../dto/update-document.dto';
import { AdminGuard } from '../auth/admin.guard';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CreateUserDto } from '../dto/create-user.dto';
import { S3Service } from 'src/s3/s3.service';
import { PrismaService } from 'src/prisma/prisma.service';

@Controller('admin')
@UseGuards(JwtAuthGuard, AdminGuard)
export class AdminController {
  constructor(
    private readonly adminService: AdminService,
    private readonly s3Service: S3Service,
    private readonly prisma: PrismaService,
  ) {}

  @Get('documents')
  getAllDocuments() {
    return this.adminService.getAllDocuments();
  }

  @Put('documents/:id')
  @UseInterceptors(FileInterceptor('file'))
  updateDocument(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateDocumentDto: UpdateDocumentDto,
    @UploadedFile() file?: Express.Multer.File,
  ) {
    return this.adminService.updateDocument(id, updateDocumentDto, file);
  }

  @Post('documents')
@UseInterceptors(FileInterceptor('file'))
async uploadDocument(
  @UploadedFile() file: Express.Multer.File,
  @Body('title') title: string,
  @Body('sharedWithId') sharedWithId: string,  // Accept as string first
  @Body('uploadedById') uploadedById: string    // Accept as string first
) {
  if (!sharedWithId || !uploadedById) {
    throw new BadRequestException('sharedWithId and uploadedById are required');
  }

  // Parse the string values to numbers
  const parsedSharedWithId = parseInt(sharedWithId, 10);
  const parsedUploadedById = parseInt(uploadedById, 10);

  // Validate that the parsing was successful
  if (isNaN(parsedSharedWithId) || isNaN(parsedUploadedById)) {
    throw new BadRequestException('sharedWithId and uploadedById must be valid numbers');
  }

  const document = await this.adminService.uploadDocument(
    file,
    title,
    parsedSharedWithId,
    parsedUploadedById
  );
    return document;
  }

  @Get('users')
  getAllUsers() {
    return this.adminService.getAllUsers();
  }



  @Post('users')
  async createUser(@Body() createUserDto: CreateUserDto) {
    return this.adminService.createUser(createUserDto);
  }

  @Post('documents/:id/share')
  async shareDocument(
    @Param('id') id: string,
    @Body() { userId }: { userId: number },
  ) {
    return this.adminService.shareDocument(+id, userId);
  }

  @Put('documents/:id/status')
  async updateDocumentStatus(
    @Param('id') id: string,
    @Body() { status }: { status: string },
  ) {
    return this.adminService.updateDocumentStatus(+id, status);
  }

  @Post('users/:id/reset-password')
  async resetUserPassword(
    @Param('id', ParseIntPipe) id: number,
    @Body() { password }: { password: string },
  ) {
    if (!password || password.length < 6) {
      throw new BadRequestException('Password must be at least 6 characters long');
    }
    return this.adminService.resetUserPassword(id, password);
  }

  // @Get('test-s3-connection')
  // async testS3Connection() {
  //   const isConnected = await this.s3Service.testConnection();
  //   if (isConnected) {
  //     return { message: 'Successfully connected to S3' };
  //   } else {
  //     return { message: 'Failed to connect to S3' };
  //   }
  // }
}