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

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

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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


	-- Utilities
local KSPathUtils = require "KSPathUtils"
local WFFileUtils = require "WFFileUtils"

	-- WF.oz_client.managed_catalog
local WFCatUser = require 'WFCatUser'
local WFCatSpec = require 'WFCatSpec'
local WFCatalog = require 'WFCatalog'

	-- WF.oz_client.sync_agent
local OzCatalogUrls = require 'OzCatalogUrls'

local LrmForceIndexUpdateModel = {}

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

function LrmForceIndexUpdateModel.makeModel(rootCatalogPath, catalogId, userId)

	-- The result of this query isn't interesting, just the side effect,
	-- that is the population of the tables mentioned in the query.

	-- Note: adding all the tables below to the actual query triggers a sqlite 'hang' bug
	-- for at least the following sqlite3 versions: 3.13.0 - 3.17.0.
	-- Workaround: list all but one of the tables in a sql comment. Good enough
	-- to trigger the indexing.

	-- TODO: jrechtie: is this still required on Android? If yes, check completeness of list.
	local sql = [[
		SELECT album_asset.docId FROM album_asset LIMIT 1
		-- albums, album_asset, assetProfileLinks, avatarCacheReferences, cacheReferences, comment, conflicts, coreInfo, coreMD5, errors, flags, importSource, needsBinaryUpload, presetsProfiles, profileRegistration, quota_exceeded, renditionRevisions, space, space_album, spacefavorite, versions, watermark, trashAssets]]

	local userInfoCachePath = KSPathUtils.child( KSPathUtils.getStandardFilePath 'caches', 'Adobe' )
	WFFileUtils.mkdir( userInfoCachePath )
	userInfoCachePath = KSPathUtils.child( userInfoCachePath, 'user' )
	WFFileUtils.mkdir( userInfoCachePath )

	local serverBaseUrl = OzCatalogUrls.baseServerUrl
	local user = WFCatUser( serverBaseUrl, userId, userInfoCachePath )
	local catSpec = WFCatSpec( user, catalogId )

	local WFPlatformInfo = require 'WFPlatformInfo'
	local dbPath = nil

	if WFPlatformInfo.isAndroid then
		local rootDir = KSPathUtils.getStandardFilePath 'carouselDocumentsInternal'
		local path = KSPathUtils.child( rootDir, catalogId )
		dbPath = KSPathUtils.child( path, "Managed Catalog.mcat" )
	end

	local catalogOptions = {
		indexSpecScript = 'LrmCatalogIndexDefinitions.lua',
		docStorePath = dbPath,
		docStoreInitProgressRequested = true,
	}

	local catalog = WFCatalog( catSpec, rootCatalogPath, catalogOptions )

	local result = catalog:indexStore():query( {
		sql = sql,
		calm = 1,
		quiesceUntilUpToDate = true
	} )
	:map( function( rows )
	
		local dummy = rows and rows[ 1 ] and rows[ 1 ][ 1 ]

		catalog:close()

		user:close()

		return dummy
		
	end )

	return {
		value = result
	}

end

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

return LrmForceIndexUpdateModel
