import { NextRequest, NextResponse } from 'next/server';
import { getDBConnexion } from '../utils';
import { v4 } from 'uuid';

export const GET = async (req: NextRequest) => {
	const userID = req.headers.get('Authorization');

	if (!userID) {
		return NextResponse.json(
			{
				error: true,
				message: 'Unauthorized request',
			},
			{
				status: 403,
			}
		);
	}

	try {
		const pool = getDBConnexion();

		const { rowCount } = await pool.query(
			`SELECT COUNT(*) FROM "user" WHERE "id"=$1 LIMIT 1;`,
			[userID]
		);
		if (rowCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Unauthorized request',
				},
				{
					status: 403,
				}
			);
		}

		const { rows } = await pool.query(
			`SELECT * FROM "point" WHERE "userid"=$1;`,
			[userID]
		);

		await pool.end();

		return NextResponse.json(rows);
	} catch (error) {
		console.error(error);
		return NextResponse.json(
			{
				error: true,
				message: 'Internal error',
			},
			{
				status: 500,
			}
		);
	}
};

export const POST = async (req: NextRequest) => {
	const userID = req.headers.get('Authorization');

	if (!userID) {
		return NextResponse.json(
			{
				error: true,
				message: 'Unauthorized request',
			},
			{
				status: 403,
			}
		);
	}

	try {
		const pool = getDBConnexion();

		const { rowCount } = await pool.query(
			`SELECT COUNT(*) FROM "user" WHERE "id"=$1 LIMIT 1;`,
			[userID]
		);
		if (rowCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Unauthorized request',
				},
				{
					status: 403,
				}
			);
		}

		const { geopoint, location, comment } = await req.json();

		if (!geopoint || !location) {
			return NextResponse.json(
				{
					error: true,
					message:
						'Missing geopoint and/or location field(s) in request body',
				},
				{
					status: 400,
				}
			);
		}

		const pointId = v4();

		await pool.query(
			'INSERT INTO "point" ("id", "userid", "geopoint", "location", "comment") VALUES ($1, $2, $3, $4, $5);',
			[pointId, userID, geopoint, location, comment]
		);

		await pool.end();

		return NextResponse.json({
			message: 'Successfully added point',
		});
	} catch (error) {
		console.error(error);
		return NextResponse.json(
			{
				error: true,
				message: 'Internal error',
			},
			{
				status: 500,
			}
		);
	}
};

export const PUT = async (req: NextRequest) => {
	const userID = req.headers.get('Authorization');

	if (!userID) {
		return NextResponse.json(
			{
				error: true,
				message: 'Unauthorized request',
			},
			{
				status: 403,
			}
		);
	}

	try {
		const pool = getDBConnexion();

		const { rowCount } = await pool.query(
			`SELECT COUNT(*) FROM "user" WHERE "id"=$1 LIMIT 1;`,
			[userID]
		);
		if (rowCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Unauthorized request',
				},
				{
					status: 403,
				}
			);
		}

		const { pointId, geopoint, location, comment } = await req.json();

		if (!pointId || !geopoint || !location) {
			return NextResponse.json(
				{
					error: true,
					message:
						'Missing pointId and/or geopoint and/or location field(s) in request body',
				},
				{
					status: 400,
				}
			);
		}

		const { rowCount: pointsCount } = await pool.query(
			`SELECT COUNT(*) FROM "point" WHERE "id"=$1 AND "userid"=$2 LIMIT 1;`,
			[pointId, userID]
		);

		if (pointsCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Point not found',
				},
				{
					status: 404,
				}
			);
		}

		await pool.query(
			'UPDATE "point" SET "geopoint"=$1, "location"=$2, "comment"=$3 WHERE "id"=$4 AND "userid"=$5;',
			[geopoint, location, comment, pointId, userID]
		);

		await pool.end();

		return NextResponse.json({
			message: 'Successfully updated point',
		});
	} catch (error) {
		console.error(error);
		return NextResponse.json(
			{
				error: true,
				message: 'Internal error',
			},
			{
				status: 500,
			}
		);
	}
};

export const DELETE = async (req: NextRequest) => {
	const userID = req.headers.get('Authorization');

	if (!userID) {
		return NextResponse.json(
			{
				error: true,
				message: 'Unauthorized request',
			},
			{
				status: 403,
			}
		);
	}

	try {
		const pool = getDBConnexion();

		const { rowCount } = await pool.query(
			`SELECT COUNT(*) FROM "user" WHERE "id"=$1 LIMIT 1;`,
			[userID]
		);
		if (rowCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Unauthorized request',
				},
				{
					status: 403,
				}
			);
		}

		const { pointId } = await req.json();

		if (!pointId) {
			return NextResponse.json(
				{
					error: true,
					message: 'Missing pointId field in request body',
				},
				{
					status: 400,
				}
			);
		}

		const { rowCount: pointsCount } = await pool.query(
			`SELECT COUNT(*) FROM "point" WHERE "id"=$1 AND "userid"=$2 LIMIT 1;`,
			[pointId, userID]
		);

		if (pointsCount != 1) {
			return NextResponse.json(
				{
					error: true,
					message: 'Point not found',
				},
				{
					status: 404,
				}
			);
		}

		await pool.query('DELETE FROM "point" WHERE "id"=$1 AND "userid"=$2;', [
			pointId,
			userID,
		]);

		await pool.end();

		return NextResponse.json({
			message: 'Successfully delete point',
		});
	} catch (error) {
		console.error(error);
		return NextResponse.json(
			{
				error: true,
				message: 'Internal error',
			},
			{
				status: 500,
			}
		);
	}
};
