import { Body, Controller, Delete, Get, HttpCode, HttpException, HttpStatus, Param, Patch, Post, Query, Req, UseGuards } from '@nestjs/common';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
import { User } from 'src/users/entities/user.entity';
import { CreateFriendshipDto } from './dto/create-friendship.dto';
import { FriendshipService } from './friendship.service';

@Controller('network')
export class FriendshipController {
	constructor(private readonly friendshipService: FriendshipService) { }

	// GET http://transcendance:8080/api/v2/network/myfriends
	@Get('myfriends')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findEmpty(@Req() req) {
		const user = req.user;
		return this.friendshipService.findAllFriends(user.id);
	}

	// GET http://transcendance:8080/api/v2/network/myfriends/relationshipId
	@Get('myfriend/:relationshipId')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findOneFriend(@Param('relationshipId') relationshipId: string, @Req() req) {
		const user = req.user;
		return this.friendshipService.findOneFriend(relationshipId, user.username);
	}

	// GET http://transcendance:8080/api/v2/network/blocked
	@Get('blocked')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findAllBlocked(@Req() req) {
		const user = req.user;
		return this.friendshipService.findAllBlockedFriends(user.username);
	}

	@Get('blocked/:relationshipId')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findOneBlocked(@Param('relationshipId') relationshipId: string, @Req() req) {
		const user = req.user;
		return this.friendshipService.findOneBlocked(relationshipId, user.username);
	}

	// POST http://transcendance:8080/api/v2/network/myfriends
	@Post('myfriends')
	@HttpCode(HttpStatus.CREATED)
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
		const user = req.user;
		if (user.username !== createFriendshipDto.receiverUsername)
			return this.friendshipService.create(createFriendshipDto, user);
		return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
	}

	// PATCH http://transcendance:8080/api/v2/network/myfriends/relationshipId/accept
	@Patch('myfriends/:relationshipId/accept')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	updateAccept(@Param('relationshipId') relationshipId: string, @Req() req)
	{
		const user : User = req.user;
		return this.friendshipService.acceptFriendship(relationshipId, user);
	}

	@Patch('myfriends/:relationshipId/decline')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	updateDecline(@Param('relationshipId') relationshipId: string, @Req() req)
	{
		const user : User = req.user;
		return this.friendshipService.declineFriendship(relationshipId, user);
	}

	@Patch('myfriends/:relationshipId/block')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	updateBlock(@Param('relationshipId') relationshipId: string, @Req() req)
	{
		const user : User = req.user;
		return this.friendshipService.blockFriendship(relationshipId, user);
	}

	// DELETE http://transcendance:8080/api/v2/network/myfriends/relationshipId
	@Delete(':relationshipId')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	remove(@Param('relationshipId') relationshipId: string, @Req() req) {
		const user : User = req.user;
		return this.friendshipService.removeFriendship(relationshipId, user);
	}

	// GET http://transcendance:8080/api/v2/network/pending
	@Get('pending')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findAllPendantFriendshipRequested(@Req() req) {
		const user = req.user;
		return this.friendshipService.findAllPendantRequestsForFriendship(user.username);
	}

	// GET http://transcendance:8080/api/v2/network/received
	@Get('received')
	@UseGuards(AuthenticateGuard)
	@UseGuards(TwoFactorGuard)
	findAllPendantFriendshipReceived(@Req() req) {
		const user = req.user;
		return this.friendshipService.findAllReceivedRequestsForFriendship(user.username);
	}
}
