package utils

import "time"

const (
	minUsername                          = "2"
	maxUsername                          = "20"
	MinUsername                          = 2
	MaxUsername                          = 20
	minPassword                          = "6"
	MinPassword                          = 6
	maxPassword                          = "64"
	MaxPassword                          = 64
	minScannerName                       = "3"
	maxScannerName                       = "16"
	MinRoleDisplayName                   = 3
	MaxRoleDisplayName                   = 30
	MaxRoleDescription                   = 80
	MaxEquipmentDocumentationTitleLength = "60"
	MaxEquipmentDocumentationNoteLength  = 2000
	MaxNoficationTitleLength             = "255"
	MinUserApiKeyNameLength              = 2
	MaxUserApiKeyNameLength              = 30

	LenHeaderXAuthorization = 36
	lenHeaderXAuthorization = "36"
	LenUserId               = 36
	LenHeaderXApiKey        = 36

	HeaderXAuthorization = "X-Authorization"
	HeaderXApiKey        = "X-Api-Key"

	MaxAvatarSize = 5 * 1024 * 1024 // 5 MB

	GroupTaskLockedTime  = 3
	SessionExpiresAtTime = 7 * 24 * 60 * 60 // 1 week

	ConnectionStateOffline = 0
	ConnectionStateOnline  = 1

	EquipmentDocumentationsPaginationLimit = 3
	GroupTasksPaginationLimit              = 5
	NotificationsPaginationLimit           = 10
	CrmCustomersPaginationLimit            = 10

	CrmPipelineCustomers       = "customers"
	CrmPipelineDmcPipeline     = "dmc-pipeline"
	CrmPipelineDmcPipelineInt  = 1 // used for database
	CrmPipelineSetterCloser    = "setter-closer"
	CrmPipelineSetterCloserInt = 2 // used for database
)

var (
	AcceptedAvatarFileTypes = []string{
		"image/png",
		"image/jpeg",
		"image/jpg",
		"image/gif"}
)

// commands sent to web clients
const (
	SentCmdInitUserSocketConnection = 1
	SentCmdUpdateConnectedUsers     = 2
	SentCmdNewGroupTaskStarted      = 3
	SentCmdNewGroupTaskStep         = 4
	SentCmdUpdateGroupTaskStep      = 5
	SentCmdUpdateGroupTask          = 6
	SentCmdReloadingGroupTasks      = 7
	SentCmdGroupTasksReloaded       = 8
	SentCmdUpdateUserSessions       = 9
	SentCmdUpdateAllUsersUserAvatar = 10
	SentCmdScannerNewScan           = 11

	SentCmdTaskLocked                                       = 16
	SentCmdTaskUnlocked                                     = 17
	SentCmdUserProfileUpdated                               = 18
	SentCmdAdminAreaNewRoleCreated                          = 19
	SentCmdAdminAreaRoleUpdated                             = 20
	SentCmdAdminAreaUpdateRoleSortingOrder                  = 21
	SentCmdAdminAreaRoleDeleted                             = 22
	SentCmdAllUsersUserRoleUpdated                          = 23
	SentCmdRolePermissionsUpdated                           = 24
	SentCmdErrorNoPermissions                               = 25
	SentCmdAllUsersNewUserCreated                           = 26
	SentCmdAllUsersUserDeleted                              = 27
	SentCmdAllUsersUserDeactivation                         = 28
	SentCmdGroupTasksCategoryGroupChanges                   = 29
	SentCmdNewUserApiKeyCreated                             = 30
	SentCmdDeletedUserApiKey                                = 31
	SentCmdNewApiKeyUsageCount                              = 32
	SentCmdInstallingPythonPackages                         = 33
	SentCmdInstallingPythonPackagesFailed                   = 34
	SentCmdInstallingPythonPackagesFinished                 = 35
	SentCmdInstallingGlobalPythonPackages                   = 36
	SentCmdInstallingGlobalPythonPackagesFailed             = 37
	SentCmdInstallingGlobalPythonPackagesFinished           = 38
	SentCmdUpdateUsers                                      = 39
	SentCmdNewNotification                                  = 41
	SentCmdAllNotificationsDeleted                          = 42
	SentCmdOneNotificationDeleted                           = 43
	SentCmdAdminAreaManageCheckedForAvailableCategories     = 44
	SentCmdAdminAreaManageLogManagerServerConnectionAdded   = 45
	SentCmdAdminAreaManageLogManagerServerConnectionRemoved = 46
	SentCmdCrmCustomerUpdated                               = 47
	SentCmdCrmCustomerCreated                               = 48
	SentCmdCrmCallProtocolCreated                           = 49
	SentCmdCrmCallProtocolDeleted                           = 50
	SentCmdCrmLinkCreated                                   = 51
	SentCmdCrmLinkUsed                                      = 52
	SentCmdCrmLinkDeleted                                   = 53
	SendCmdCustomerFeedbackAddFeedback                      = 54
)

// commands received from web clients
const (
	ReceivedCmdStartGroupTasks                 = 1
	ReceivedCmdTaskFailedTryAgainRunTaskStep   = 2
	ReceivedCmdTaskContinueTaskStep            = 3
	ReceivedCmdReloadGroupTasks                = 4
	ReceivedCmdTaskLocking                     = 5
	ReceivedCmdUpdateUserProfile               = 6
	ReceivedCmdAdminAreaCreateNewRole          = 7
	ReceivedCmdAdminAreaUpdateRole             = 8
	ReceivedCmdAdminAreaUpdateRoleSortingOrder = 9
	ReceivedCmdAdminAreaDeleteRole             = 10
	ReceivedCmdAllUsersUpdateUserRole          = 11
	ReceivedCmdAllUsersCreateNewUser           = 12
	ReceivedCmdAllUsersDeleteUser              = 13
	ReceivedCmdAllUsersUserDeactivation        = 14
	ReceivedCmdScannerNewScan                  = 15

	ReceivedCmdHandleUserActionTaskStep                        = 18
	ReceivedCmdCreateNewUserApiKey                             = 19
	ReceivedCmdDeleteUserApiKey                                = 20
	ReceivedCmdGroupTasksInstallPythonPackages                 = 21
	ReceivedCmdGroupTasksInstallGlobalPythonPackages           = 22
	ReceivedCmdSubscribeToTopic                                = 23
	ReceivedCmdDeleteAllNotifications                          = 24
	ReceivedCmdDeleteOneNotification                           = 25
	ReceivedCmdAdminAreaManageCheckWhichCategoriesAreAvailable = 26
	ReceivedCmdAdminAreaManageAddLogManagerServerConnection    = 27
	ReceivedCmdAdminAreaManageDeleteLogManagerServerConnection = 28
)

const (
	GroupTasksStatusFinished      uint8 = 1
	GroupTasksStatusRunning       uint8 = 2
	GroupTasksStatusCanceled      uint8 = 3
	GroupTasksStatusFailed        uint8 = 4
	GroupTasksStatusInputRequired uint8 = 5
	GroupTasksStatusPaused        uint8 = 6
	GroupTasksStatusUndoEnded     uint8 = 7
)

var (
	generalRules = map[string]string{
		"Username":    "required,min=" + minUsername + ",max=" + maxUsername,
		"Password":    "required", // length is checked later because sent in base64
		"SessionId":   "required,len=" + lenHeaderXAuthorization,
		"ScannerName": "required,min=" + minScannerName + ",max=" + maxScannerName,
	}

	groupTaskRules = map[string]string{
		"Category":     "required",
		"GroupId":      "required",
		"Description":  "required",
		"GlobalInputs": "required",
	}

	equipmentRules = map[string]string{
		"StockItemId": "required,numericstring",
		"Type":        "required,number",
		"Title":       "required,max=" + MaxEquipmentDocumentationTitleLength,
		"Notes":       "required",
	}

	notificationRules = map[string]string{
		"Type":  "required,number",
		"Title": "required,max=" + MaxNoficationTitleLength,
	}
)

const (
	_equipmentDocumentation                             = "equipment_documentation."
	PermissionEquipmentDocumentationView                = _equipmentDocumentation + "view"
	PermissionEquipmentDocumentationEdit                = _equipmentDocumentation + "edit"
	PermissionEquipmentDocumentationCreate              = _equipmentDocumentation + "create"
	_groupTasks                                         = "group_tasks."
	PermissionGroupTasksOverviewXYNewTask               = _groupTasks + "overview.XY.new_task"
	PermissionGroupTasksOverviewXYReloadGroupConfig     = _groupTasks + "overview.XY.reload_group_config"
	PermissionGroupTasksOverviewXYInstallPythonPackages = _groupTasks + "overview.XY.install_python_packages"
	PermissionGroupTasksOverviewXYView                  = _groupTasks + "overview.XY.view"
	PermissionGroupTasksHistory                         = _groupTasks + "history"
	PermissionGroupTasksInstallGlobalPythonPackages     = _groupTasks + "install_global_python_packages"
	PermissionGroupTasksCheckingForCategoryGroupChanges = _groupTasks + "checking_for_category_group_changes"
	PermissionAllUsersCreateNewUser                     = "all_users.create_new_user"
	PermissionAllUsersActionChangeRole                  = "all_users.action.change_role"
	PermissionAllUsersActionDeleteUser                  = "all_users.action.delete_user"
	PermissionAllUsersActionUserDeactivation            = "all_users.action.user_deactivation"

	_adminArea                                                = "admin_area."
	_adminAreaRoles                                           = _adminArea + "roles."
	PermissionAdminAreaCreateNewRole                          = _adminAreaRoles + "create_new_role"
	PermissionAdminAreaUpdateRole                             = _adminAreaRoles + "update_role"
	PermissionAdminAreaDeleteRole                             = _adminAreaRoles + "delete_role"
	PermissionAdminAreaMoveRoleUpDown                         = _adminAreaRoles + "move_role_up_down"
	PermissionAdminAreaLogs                                   = _adminArea + "logs"
	_adminAreaManage                                          = _adminArea + "manage."
	PermissionAdminAreaManageCheckWhichCategoriesAreAvailable = _adminAreaManage + "check_which_categories_are_available"
	PermissionAdminAreaManageAddLogManagerServerConnection    = _adminAreaManage + "add_log_manager_server_connection"
	PermissionAdminAreaManageRemoveLogManagerServerConnection = _adminAreaManage + "remove_log_manager_server_connection"

	PermissionUserProfileApiKeys = "user_profile.api_keys"
	_consoles                    = "consoles."
	PermissionConsolesView       = _consoles + "view"

	PermissionRoboticsRobotsView                             = "robotics.robots.view"
	PermissionsRoboticsRobotsViewRobotsAddresses             = "robotics.robots.view_robots_addresses"
	PermisionsRoboticsRobotsFreeUpJob                        = "robotics.robots.free_up_job"
	PermissionsRoboticsRobotsEditRobotName                   = "robotics.robots.edit_robot_name"
	PermissionsRoboticsRobotsDisconnectRobot                 = "robotics.robots.disconnect_robot"
	PermissionsRoboticsRobotsEnablePermitJoin                = "robotics.robots.enable_permit_join"
	PermissionsRoboticsRobotsAuthorizeDenyUnauthorizedRobots = "robotics.robots.authorize_deny_unauthorized_robots"
	PermissionsRoboticsRobotsViewSwaggerDocumentation        = "robotics.robots.view_swagger_documentation"

	_crm                            = "crm."
	PermissionCrmCustomersView      = _crm + "customers.view"
	PermissionCrmCustomersEdit      = _crm + "customers.edit"
	PermissionCrmCustomersCreate    = _crm + "customers.create"
	PermissionCrmDmcPipelineView    = _crm + "dmc_pipeline.view"
	PermissionCrmSetterCloserView   = _crm + "setter_closer.view"
	PermissionCrmCallProtocolCreate = _crm + "call_protocol.create"
	PermissionCrmCallProtocolView   = _crm + "call_protocol.view"
	PermissionCrmCallProtocolDelete = _crm + "call_protocol.delete"
	PermissionCrmLinkCreate         = _crm + "link.create"
	PermissionCrmLinkView           = _crm + "link.view"
	PermissionCrmLinkDelete         = _crm + "link.delete"

	_customerFeedback              = "customerfeedback."
	PermissionCustomerFeedbackView = _customerFeedback + "view"
)

var SystemPermissions = []string{
	PermissionEquipmentDocumentationView,
	PermissionEquipmentDocumentationEdit,
	PermissionEquipmentDocumentationCreate,
	PermissionGroupTasksHistory,
	PermissionGroupTasksInstallGlobalPythonPackages,
	PermissionGroupTasksCheckingForCategoryGroupChanges,
	PermissionAllUsersActionChangeRole,
	PermissionAllUsersActionDeleteUser,
	PermissionAllUsersActionUserDeactivation,
	PermissionAllUsersCreateNewUser,
	PermissionAdminAreaCreateNewRole,
	PermissionAdminAreaUpdateRole,
	PermissionAdminAreaDeleteRole,
	PermissionAdminAreaMoveRoleUpDown,
	PermissionAdminAreaLogs,
	PermissionAdminAreaManageCheckWhichCategoriesAreAvailable,
	PermissionAdminAreaManageAddLogManagerServerConnection,
	PermissionAdminAreaManageRemoveLogManagerServerConnection,
	PermissionUserProfileApiKeys,
	PermissionConsolesView,
	PermissionRoboticsRobotsView,
	PermissionsRoboticsRobotsViewRobotsAddresses,
	PermisionsRoboticsRobotsFreeUpJob,
	PermissionsRoboticsRobotsEditRobotName,
	PermissionsRoboticsRobotsDisconnectRobot,
	PermissionsRoboticsRobotsEnablePermitJoin,
	PermissionsRoboticsRobotsAuthorizeDenyUnauthorizedRobots,
	PermissionsRoboticsRobotsViewSwaggerDocumentation,
	PermissionCrmCustomersView,
	PermissionCrmCustomersEdit,
	PermissionCrmCustomersCreate,
	PermissionCrmDmcPipelineView,
	PermissionCrmSetterCloserView,
	PermissionCrmCallProtocolCreate,
	PermissionCrmCallProtocolView,
	PermissionCrmCallProtocolDelete,
	PermissionCrmLinkCreate,
	PermissionCrmLinkView,
	PermissionCrmLinkDelete,
	PermissionCustomerFeedbackView,
}

var DynamicGroupTasksPermissions = []string{
	PermissionGroupTasksOverviewXYNewTask,
	PermissionGroupTasksOverviewXYReloadGroupConfig,
	PermissionGroupTasksOverviewXYInstallPythonPackages,
	PermissionGroupTasksOverviewXYView,
}

// This represents the paths registered on the api.
// Available paths which are used for the websocket topic subscription
// to only send messages to clients who are subscribed to the topic
const (
	// this is path is dynamic because there are more params after the /v1/grouptasks/
	SubscribedDynamicTopicGroupTasks = "/group-tasks/"

	// these paths are static - there are no params after the path
	SubscribedTopicAdminAreaRoles = "/admin-area/roles"
	SubscribedTopicUsers          = "/users"
	SubscribedTopicUserProfile    = "/user-profile"

	SubscribedTopicConsoles = "/consoles"

	SubscribedTopicAdminAreaManage = "/admin-area/manage"

	SubscribedTopicCrm = "/crm/"

	SubscribedTopicCustomerFeedback = "/customer-feedback/"
)

var ProductPipelineCacheTime = 2 * time.Hour
