mod parse;
use parse::Parse;
use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::kvs::Datastore;
use surrealdb::sql::Value;

#[tokio::test]
async fn subquery_select() -> Result<(), Error> {
	let sql = "
		-- Create a record
		CREATE person:test SET name = 'Tobie', age = 21;
		-- Select all records, returning an array
		SELECT age >= 18 as adult FROM person;
		-- Select a specific record, still returning an array
		SELECT age >= 18 as adult FROM person:test;
		-- Select all records in a subquery, returning an array
		RETURN (SELECT age >= 18 AS adult FROM person);
		-- Select a specific record in a subquery, returning an object
		RETURN (SELECT age >= 18 AS adult FROM person:test);
		-- Using an outer SELECT, select all records in a subquery, returning an array
		SELECT * FROM (SELECT age >= 18 AS adult FROM person) WHERE adult = true;
		-- Using an outer SELECT, select a specific record in a subquery, returning an array
		SELECT * FROM (SELECT age >= 18 AS adult FROM person:test) WHERE adult = true;
	";
	let dbs = Datastore::new("memory").await?;
	let ses = Session::for_kv().with_ns("test").with_db("test");
	let res = &mut dbs.execute(&sql, &ses, None, false).await?;
	assert_eq!(res.len(), 7);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				age: 21,
				id: person:test,
				name: 'Tobie'
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				adult: true
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				adult: true
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				adult: true
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			adult: true
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				adult: true
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"[
			{
				adult: true
			}
		]",
	);
	assert_eq!(tmp, val);
	//
	Ok(())
}

#[tokio::test]
async fn subquery_ifelse_set() -> Result<(), Error> {
	let sql = "
		-- Check if the record exists
		LET $record = (SELECT *, count() AS count FROM person:test);
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			(UPDATE person:test SET sport +?= 'football' RETURN sport)
		ELSE
			(UPDATE person:test SET sport = ['basketball'] RETURN sport)
		END;
		-- Check if the record exists
		LET $record = SELECT *, count() AS count FROM person:test;
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			UPDATE person:test SET sport +?= 'football' RETURN sport
		ELSE
			UPDATE person:test SET sport = ['basketball'] RETURN sport
		END;
		-- Check if the record exists
		LET $record = SELECT *, count() AS count FROM person:test;
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			UPDATE person:test SET sport +?= 'football' RETURN sport;
		ELSE
			UPDATE person:test SET sport = ['basketball'] RETURN sport;
		END;
	";
	let dbs = Datastore::new("memory").await?;
	let ses = Session::for_kv().with_ns("test").with_db("test");
	let res = &mut dbs.execute(&sql, &ses, None, false).await?;
	assert_eq!(res.len(), 9);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			count: 1,
			id: person:test,
			sport: [
				'basketball',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			count: 1,
			id: person:test,
			sport: [
				'basketball',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	Ok(())
}

#[tokio::test]
async fn subquery_ifelse_array() -> Result<(), Error> {
	let sql = "
		-- Check if the record exists
		LET $record = (SELECT *, count() AS count FROM person:test);
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			(UPDATE person:test SET sport += 'football' RETURN sport)
		ELSE
			(UPDATE person:test SET sport = ['basketball'] RETURN sport)
		END;
		-- Check if the record exists
		LET $record = SELECT *, count() AS count FROM person:test;
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			UPDATE person:test SET sport += 'football' RETURN sport
		ELSE
			UPDATE person:test SET sport = ['basketball'] RETURN sport
		END;
		-- Check if the record exists
		LET $record = SELECT *, count() AS count FROM person:test;
		-- Return the specified record
		RETURN $record;
		-- Update the record field if it exists
		IF $record.count THEN
			UPDATE person:test SET sport += 'football' RETURN sport;
		ELSE
			UPDATE person:test SET sport = ['basketball'] RETURN sport;
		END;
	";
	let dbs = Datastore::new("memory").await?;
	let ses = Session::for_kv().with_ns("test").with_db("test");
	let res = &mut dbs.execute(&sql, &ses, None, false).await?;
	assert_eq!(res.len(), 9);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			count: 1,
			id: person:test,
			sport: [
				'basketball',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::None;
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			count: 1,
			id: person:test,
			sport: [
				'basketball',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	let tmp = res.remove(0).result?;
	let val = Value::parse(
		"{
			sport: [
				'basketball',
				'football',
				'football',
			]
		}",
	);
	assert_eq!(tmp, val);
	//
	Ok(())
}