--[[----------------------------------------------------------------------------

WFCatImportManifestStatusQueries.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2017 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

local WFCatImportManifestQueriesUtils = require 'WFCatImportManifestQueriesUtils'
local WFCatImportManifestConstants = require 'WFCatImportManifestConstants'

local inputField = WFCatImportManifestQueriesUtils.inputField

--------------------------------------------------------------------------------

	-- Table creation

local createQueries = {
					
	assetsTable = "CREATE TABLE assets (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",
	
	copiesTable = "CREATE TABLE copies (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" status INTEGER" ..
				" )",
	
	allRootAlbumAssetsTable = "CREATE TABLE all_root_album_assets (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",
	
	-- Status of root albums are also added to the
	-- albums table
	albumsTable = "CREATE TABLE albums (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" oz_name TEXT," ..
					" status INTEGER" ..
				" )",

	album_assets = "CREATE TABLE album_assets (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",

	personTable = "CREATE TABLE persons (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",

	facesTable = "CREATE TABLE faces (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",
	
	stacksTable = "CREATE TABLE stacks (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" oz_id TEXT," ..
					" status INTEGER" ..
				" )",
	
	stack_asset = "CREATE TABLE stack_asset (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" status INTEGER" ..
				" )",

	stacksAlbumTable = "CREATE TABLE stack_album (" ..
					" local_id INTEGER PRIMARY KEY," ..
					" status INTEGER" ..
				" )",

}

--------------------------------------------------------------------------------

local writeOperations = {
	
	updateAssetStatus = {
		sql = "UPDATE assets SET status = ? WHERE local_id = ?",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'status', 'integer', true ),
			inputField( 'local_id', 'integer', true ),
		},
	},
	
	addOrReplaceAssetStatus = {
		sql = "INSERT OR REPLACE INTO assets( local_id, oz_id, status ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
			inputField( 'status', 'integer', true ),
		},
	},
	

	updateAlbumStatus = {
		sql = "UPDATE albums set status = ? WHERE local_id = ?",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'status', 'integer', true ),
			inputField( 'local_id', 'integer', true ),
		},
	},
	
	addOrReplaceAlbumStatus = {
		sql = "INSERT OR REPLACE INTO albums( local_id, oz_id, oz_name, status ) values( ?, ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
			inputField( 'oz_name', 'text', false ),
			inputField( 'status', 'integer', true ),
		},
	},
	
	addRootAlbumAssetStatus = {
		sql = "INSERT OR REPLACE INTO all_root_album_assets( local_id, status, oz_id ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'status', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
		},
	},

	addAlbumAssetStatus = {
		sql = "INSERT INTO album_assets( local_id, status, oz_id ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'status', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
		},
	},

	addCopyStatus = {
		sql = "INSERT INTO copies( local_id, status ) values( ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'status', 'integer', true ),
		},
	},
	
	addOrReplacePersonStatus = {
		sql = "INSERT OR REPLACE INTO persons( local_id, oz_id, status ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
			inputField( 'status', 'integer', true ),
		},
	},

	updatePersonStatus = {
		sql = "UPDATE persons SET status = ? WHERE local_id = ?",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'status', 'integer', true ),
			inputField( 'local_id', 'integer', true ),
		},
	},

	addOrReplaceFacesStatus = {
		sql = "INSERT OR REPLACE INTO faces( local_id, oz_id, status ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
			inputField( 'status', 'integer', true ),
		},
	},
	
	updateFacesStatus = {
		sql = "UPDATE faces SET status = ? WHERE local_id = ?",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'status', 'integer', true ),
			inputField( 'local_id', 'integer', true ),
		},
	},
	
	updateStackStatus = {
		sql = "UPDATE stacks SET status = ? WHERE local_id = ?",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'status', 'integer', true ),
			inputField( 'local_id', 'integer', true ),
		},
	},
	
	addOrReplaceStackStatus = {
		sql = "INSERT OR REPLACE INTO stacks( local_id, oz_id, status ) values( ?, ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'oz_id', 'uuid', false ),
			inputField( 'status', 'integer', true ),
		},
	},
	
	addOrReplaceStackAssetStatus = {
		sql = "INSERT OR REPLACE INTO stack_asset( local_id, status ) values( ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'status', 'integer', true ),
		},
	},
	
	addOrReplaceStackAlbumStatus = {
		sql = "INSERT OR REPLACE INTO stack_album( local_id, status ) values( ?, ? )",
		batchSize = 1000,
		inputFieldsInOrder = {
			inputField( 'local_id', 'integer', true ),
			inputField( 'status', 'integer', true ),
		},
	},

}

--------------------------------------------------------------------------------

local deleteOperations = {}	

--------------------------------------------------------------------------------

local readOperations = {

	selectAssetsStatus = {
		sql = "SELECT local_id, oz_id, status from assets",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'status' },
	},
	
	selectAssetStatusWithStatus = {
		sql = "SELECT local_id, oz_id FROM assets WHERE status = ?",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id' },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	countAssetsStatus = {
		sql = "SELECT count( * ) from assets WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},

	selectAlbumsStatus = {
		sql = "SELECT local_id, oz_id, oz_name, status from albums",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'oz_name', 'status' },
	},
	
	selectRootAlbumsStatus = {
		sql = "SELECT local_id, oz_id, oz_name, status from albums WHERE local_id IN( "..WFCatImportManifestConstants.rootAlbumsWithComma.." )",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'oz_name', 'status' },
	},
	
	selectAlbumStatusWithStatus = {
		sql = "SELECT local_id, oz_id, oz_name FROM albums WHERE status = ?",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'oz_name' },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	countAlbumsStatus = {
		sql = "SELECT count( * ) from albums WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	selectRootAlbumAssetsStatus = {
		sql = "SELECT local_id, status from all_root_album_assets",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'status' },
	},

	selectAlbumAssetsStatus = {
		sql = "SELECT local_id, status from album_assets",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'status' },
	},
	
	countAlbumAssetsStatus = {
		sql = "SELECT count( * ) from album_assets WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},

	selectCopiesStatus = {
		sql = "SELECT local_id, status from copies",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'status' },
	},
	
	countCopiesStatus = {
		sql = "SELECT count( * ) from copies WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},

	selectPersonStatus = {
		sql = "SELECT local_id, oz_id, status FROM persons",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'status' },
	},
	
	selectPersonStatusWithStatus = {
		sql = "SELECT local_id, oz_id FROM persons WHERE status = ?",
		batchSize = 100,
		outputFieldsInOrder = { 'local_id', 'oz_id' },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	countPersonStatus = {
		sql = "SELECT count( * ) from persons WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	selectFacesStatus = {
		sql = "SELECT local_id, oz_id, status FROM faces",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'status' },
	},
	
	countFacesStatus = {
		sql = "SELECT count( * ) from faces WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	selectStackStatus = {
		sql = "SELECT local_id, oz_id, status FROM stacks",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'oz_id', 'status' },
	},
	
	selectStackStatusWithStatus = {
		sql = "SELECT local_id, oz_id FROM stacks WHERE status = ?",
		batchSize = 100,
		outputFieldsInOrder = { 'local_id', 'oz_id' },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	countStacksStatus = {
		sql = "SELECT count( * ) from stacks WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	selectStackAlbumStatus = {
		sql = "SELECT local_id, status FROM stack_album",
		batchSize = 1000,
		outputFieldsInOrder = { 'local_id', 'status' },
	},
	
	countStackAlbumsStatus = {
		sql = "SELECT count( * ) from stack_album WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
	countStackAssetsStatus = {
		sql = "SELECT count( * ) from stack_asset WHERE status = ?",
		outputFieldsInOrder = { "count" },
		conditionFieldsInOrder = {
			inputField( 'status', 'integer', true ),
		},
	},
	
}

--------------------------------------------------------------------------------

local function getCreateQueries()
	return createQueries
end

--------------------------------------------------------------------------------

local function getWriteQuery( queryName )
	return writeOperations[ queryName ]
end

--------------------------------------------------------------------------------

local function getReadQuery( queryName )
	return readOperations[ queryName ]
end

--------------------------------------------------------------------------------

local function getDeleteQuery( queryName )
	return deleteOperations[ queryName ]
end

--------------------------------------------------------------------------------

local function getWriteOperations()
	return writeOperations
end

--------------------------------------------------------------------------------

local function getReadOperations()
	return readOperations
end

--------------------------------------------------------------------------------

local function getDeleteOperations()
	return deleteOperations
end

--------------------------------------------------------------------------------

return {
	getCreateQueries = getCreateQueries,
	getWriteQuery = getWriteQuery,
	getReadQuery = getReadQuery,
	getDeleteQuery = getDeleteQuery,
	getWriteOperations = getWriteOperations,
	getReadOperations = getReadOperations,
	getDeleteOperations = getDeleteOperations,
}
