UserService.kt

package fr.codetics.ev_server.services.people

import fr.codetics.ev_server.core.AppPrincipal
import fr.codetics.ev_server.core.AppProperties
import fr.codetics.ev_server.core.Csv
import fr.codetics.ev_server.core.Database
import fr.codetics.ev_server.core.I18n
import fr.codetics.ev_server.core.ImportError
import fr.codetics.ev_server.core.ImportResult
import fr.codetics.ev_server.core.NotFound
import fr.codetics.ev_server.core.ensure
import fr.codetics.ev_server.core.msg
import fr.codetics.ev_server.core.pageRequest
import fr.codetics.ev_server.models.api.MutationResponse
import fr.codetics.ev_server.models.api.PageResponse
import fr.codetics.ev_server.models.entities.ChargingTokenEntity
import fr.codetics.ev_server.models.entities.ChargingTokenKey
import fr.codetics.ev_server.models.entities.SiteUserEntity
import fr.codetics.ev_server.models.entities.SiteUserKey
import fr.codetics.ev_server.models.entities.UserEntity
import fr.codetics.ev_server.models.notifications.NotificationPreferences
import fr.codetics.ev_server.models.notifications.NotificationPreferencesInput
import fr.codetics.ev_server.models.people.ChallengeKind
import fr.codetics.ev_server.models.people.CreatedUser
import fr.codetics.ev_server.models.people.ProfileInput
import fr.codetics.ev_server.models.people.SiteAssignmentInput
import fr.codetics.ev_server.models.people.SiteUserResponse
import fr.codetics.ev_server.models.people.UserInError
import fr.codetics.ev_server.models.people.UserInput
import fr.codetics.ev_server.models.people.UserResponse
import fr.codetics.ev_server.models.people.UserRole
import fr.codetics.ev_server.models.people.UserSiteResponse
import fr.codetics.ev_server.persistence.Condition
import fr.codetics.ev_server.persistence.Queries
import fr.codetics.ev_server.persistence.anyOf
import fr.codetics.ev_server.persistence.contains
import fr.codetics.ev_server.persistence.eq
import fr.codetics.ev_server.persistence.list
import fr.codetics.ev_server.persistence.page
import fr.codetics.ev_server.repositories.AccessTokenRepository
import fr.codetics.ev_server.repositories.ChargingTokenRepository
import fr.codetics.ev_server.repositories.OAuthIdentityRepository
import fr.codetics.ev_server.repositories.SiteUserRepository
import fr.codetics.ev_server.repositories.TenantRepository
import fr.codetics.ev_server.repositories.TransactionRepository
import fr.codetics.ev_server.services.notifications.PushService
import fr.codetics.ev_server.services.stations.StationPhotoService
import fr.codetics.ev_server.services.tokens.CardOrderService
import fr.codetics.ev_server.repositories.UserRepository
import fr.codetics.ev_server.repositories.WorkspacePaymentSettingsRepository
import fr.codetics.ev_server.security.AccountService
import fr.codetics.ev_server.security.EULA_VERSION
import fr.codetics.ev_server.security.validateEmail
import fr.codetics.ev_server.services.organization.Organization
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.UUID
import org.springframework.context.ApplicationEventPublisher
import org.springframework.context.event.EventListener
import org.springframework.data.domain.Sort
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

data class UserFilter(val search: String = "", val role: UserRole? = null, val state: String? = null)

@Service
class UserService(
    private val db: Database,
    private val users: UserRepository,
    private val accessTokens: AccessTokenRepository,
    private val identities: OAuthIdentityRepository,
    private val chargingTokens: ChargingTokenRepository,
    private val siteUsers: SiteUserRepository,
    private val tenants: TenantRepository,
    private val paymentSettings: WorkspacePaymentSettingsRepository,
    private val organization: Organization,
    private val accounts: AccountService,
    private val queries: Queries,
    private val properties: AppProperties,
    private val events: ApplicationEventPublisher,
    private val transactions: TransactionRepository,
    private val photos: StationPhotoService,
    private val push: PushService,
    private val cardOrders: CardOrderService,
) {
    fun list(user: AppPrincipal, offset: Int = 0, limit: Int = 500, filter: UserFilter = UserFilter()): PageResponse<UserResponse> {
        val page =
            queries.page<UserEntity>(conditions(user.tenantId, filter), pageRequest(offset, limit, Sort.by("displayName", "email")))
        return PageResponse(responses(user.tenantId, page.content), page.totalElements)
    }

    private fun conditions(tenant: UUID, filter: UserFilter): List<Condition> {
        ensure(filter.search.length <= 200) { msg("validation.search_too_long") }
        val conditions = mutableListOf<Condition>(eq("tenantId", tenant), eq("deletedAt", null))
        if (filter.search.isNotBlank()) {
            conditions += anyOf(contains("email", filter.search.trim()), contains("displayName", filter.search.trim()))
        }
        filter.role?.let { conditions += eq("role", it) }
        when (filter.state) {
            "active" -> conditions += listOf(eq("enabled", true), eq("emailVerified", true))
            "disabled" -> conditions += eq("enabled", false)
            "unverified" -> conditions += eq("emailVerified", false)
            null, "" -> Unit
            else -> throw ResponseStatusException(HttpStatus.BAD_REQUEST, msg("validation.unknown_status_filter"))
        }
        return conditions
    }

    private fun responses(tenant: UUID, accounts: List<UserEntity>): List<UserResponse> {
        val counts = siteUsers.findAllByKeyTenantId(tenant).groupingBy { it.key.userId }.eachCount()
        val managers =
            users.findAllByTenantIdAndIdIn(tenant, accounts.mapNotNull { it.managerId }.toSet()).associate { it.id to it.displayName }
        return accounts.map { UserResponse.from(it, counts[it.id] ?: 0, EULA_VERSION, it.managerId?.let(managers::get)) }
    }

    fun find(user: AppPrincipal, id: UUID): UserResponse = responses(user.tenantId, listOf(account(user.tenantId, id))).first()

    fun account(tenant: UUID, id: UUID): UserEntity =
        users.findByTenantIdAndId(tenant, id)?.takeIf { it.deletedAt == null } ?: throw NotFound(msg("users.not_found"))

    fun create(body: UserInput, user: AppPrincipal): CreatedUser {
        validateUser(body.email, body.name, body.phone, body.locale)
        validateEmployment(user.tenantId, null, body)

        val created =
            db.atomic {
                if (users.findByTenantIdAndEmail(user.tenantId, body.email.trim().lowercase()) != null) {
                    throw ResponseStatusException(HttpStatus.CONFLICT, msg("users.email_exists"))
                }
                val account =
                    users.save(
                        UserEntity(
                            tenantId = user.tenantId,
                            email = body.email.trim().lowercase(),
                            displayName = body.name.trim(),
                            role = body.role,
                            enabled = body.enabled,
                            phone = body.phone?.trim()?.ifBlank { null },
                            locale = body.locale ?: I18n.locale().language,
                            managerId = body.managerId,
                            costCenter = body.costCenter?.trim()?.ifBlank { null },
                            employeeNumber = body.employeeNumber?.trim()?.ifBlank { null },
                        )
                    ).also { db.flush() }
                assignAutomaticSites(account)
                db.audit(user.tenantId, user.id.toString(), "USER_CREATE", account.id.toString())
                account
            }
        invite(created)
        return CreatedUser(created.id)
    }

    fun invite(account: UserEntity) {
        val slug = tenants.findById(account.tenantId).orElse(null)?.slug ?: return
        runCatching { accounts.requestChallenge(slug, account.email, ChallengeKind.RESET) }
    }

    @EventListener
    fun registered(event: UserRegistered) = assignAutomaticSites(event.user)

    fun assignAutomaticSites(account: UserEntity) {
        organization.sites(account.tenantId).filter { it.data.autoUserAssignment }.forEach { site ->
            val key = SiteUserKey(account.tenantId, site.id, account.id)
            if (!siteUsers.existsById(key)) siteUsers.save(SiteUserEntity(key))
        }
    }

    fun update(id: UUID, body: UserInput, user: AppPrincipal): MutationResponse {
        validateUser(body.email, body.name, body.phone, body.locale)
        ensure(id != user.id || (body.enabled && body.role == UserRole.ADMIN)) {
            msg("users.cannot_demote_self")
        }
        validateEmployment(user.tenantId, id, body)

        return db.atomic {
            val account = account(user.tenantId, id)
            val statusChanged = account.enabled != body.enabled
            account.displayName = body.name.trim()
            account.role = body.role
            account.enabled = body.enabled
            account.phone = body.phone?.trim()?.ifBlank { null }
            body.locale?.let { account.locale = it }
            account.managerId = body.managerId
            account.costCenter = body.costCenter?.trim()?.ifBlank { null }
            account.employeeNumber = body.employeeNumber?.trim()?.ifBlank { null }
            users.save(account)
            accessTokens.deleteAllByUserId(id)
            db.audit(user.tenantId, user.id.toString(), "USER_UPDATE", id.toString())
            if (statusChanged) events.publishEvent(UserStatusChanged(account, account.enabled))
            MutationResponse()
        }
    }

    fun delete(id: UUID, user: AppPrincipal): MutationResponse {
        ensure(id != user.id) { msg("users.cannot_delete_self") }
        return erase(id, user)
    }

    fun deleteOwn(user: AppPrincipal): MutationResponse {
        ensure(user.role != UserRole.ADMIN) { msg("users.admin_cannot_delete_self") }
        if (transactions.findAllByTenantIdAndStoppedAtIsNull(user.tenantId).any { it.userId == user.id }) {
            throw ResponseStatusException(HttpStatus.CONFLICT, msg("users.finish_session_first"))
        }
        return erase(user.id, user)
    }

    private fun erase(id: UUID, user: AppPrincipal): MutationResponse {
        return db.atomic {
            val account = account(user.tenantId, id)
            photos.deleteAllOf(user.tenantId, id)
            cardOrders.forget(user.tenantId, id)
            push.forget(id)
            chargingTokens.findAllByKeyTenantIdAndUserId(user.tenantId, id).forEach { token ->
                if (token.vehicleId != null) {
                    chargingTokens.delete(token)
                    return@forEach
                }
                token.userId = null
                token.enabled = false
                token.defaultToken = false
                chargingTokens.save(token)
            }
            siteUsers.deleteAll(siteUsers.findAllByKeyTenantIdAndKeyUserId(user.tenantId, id))
            accessTokens.deleteAllByUserId(id)
            identities.deleteAllByUserId(id)
            account.email = "supprime-${account.id}@invalid.local"
            account.displayName = "Utilisateur supprimé"
            account.passwordHash = null
            account.phone = null
            account.enabled = false
            account.emailVerified = false
            account.notificationPreferences = "{}"
            account.deletedAt = Instant.now()
            users.save(account)
            db.audit(user.tenantId, user.id.toString(), "USER_DELETE", id.toString())
            MutationResponse()
        }
    }

    fun inError(user: AppPrincipal): List<UserInError> {
        val accounts = queries.list<UserEntity>(conditions(user.tenantId, UserFilter()), Sort.by("displayName"))
        val withBadge =
            chargingTokens.findAllByKeyTenantId(user.tenantId, org.springframework.data.domain.PageRequest.of(0, 100000))
                .content
                .mapNotNull { it.userId }
                .toSet()
        val billing = paymentSettings.findById(user.tenantId).map { it.userBillingEnabled }.orElse(false)
        val inactiveBefore = Instant.now().minus(properties.supervision.inactivityMonths * 30, ChronoUnit.DAYS)
        val failing =
            accounts.mapNotNull { account ->
                val errors = buildList {
                    if (!account.emailVerified) add("UNVERIFIED")
                    if (!account.enabled) add("DISABLED")
                    if (account.role.selfScoped && account.id !in withBadge) add("NO_BADGE")
                    if ((account.lastLoginAt ?: account.createdAt).isBefore(inactiveBefore)) add("INACTIVE")
                    if (billing && account.role.selfScoped && account.stripeCustomerId == null) add("NO_PAYMENT_METHOD")
                }
                if (errors.isEmpty()) null else account to errors
            }
        val views = responses(user.tenantId, failing.map { it.first })
        return views.zip(failing).map { (view, entry) -> UserInError(view, entry.second) }
    }

    fun export(user: AppPrincipal) =
        Csv.download(
            "utilisateurs",
            listOf(
                "email", "nom", "role", "actif", "verifie", "telephone", "langue", "cree_le", "derniere_connexion", "badges",
                "matricule", "centre_de_cout", "responsable",
            ),
            queries.list<UserEntity>(conditions(user.tenantId, UserFilter()), Sort.by("displayName")).asSequence().map { account ->
                listOf(
                    account.email,
                    account.displayName,
                    account.role,
                    if (account.enabled) "oui" else "non",
                    if (account.emailVerified) "oui" else "non",
                    account.phone,
                    account.locale,
                    account.createdAt,
                    account.lastLoginAt,
                    chargingTokens.findAllByKeyTenantIdAndUserId(user.tenantId, account.id).joinToString(" ") { it.key.uid },
                    account.employeeNumber,
                    account.costCenter,
                    account.managerId?.let { users.findByTenantIdAndId(user.tenantId, it)?.email },
                )
            },
        )

    fun import(user: AppPrincipal, content: String): ImportResult {
        val rows = Csv.read(content, 5000)
        var created = 0
        var skipped = 0
        val errors = mutableListOf<ImportError>()
        val invitations = mutableListOf<UserEntity>()
        rows.forEachIndexed { index, row ->
            val line = index + 2
            try {
                val email = row["email"].orEmpty().trim().lowercase()
                val name = (row["name"] ?: row["nom"]).orEmpty().trim().ifBlank { email }
                val role = (row["role"] ?: row["rôle"]).orEmpty().trim().uppercase().ifBlank { "DRIVER" }
                val badge = (row["badge"] ?: row["uid"]).orEmpty().trim()
                val employeeNumber = (row["matricule"] ?: row["employeenumber"])?.trim()?.ifBlank { null }
                val costCenter = (row["centre_de_cout"] ?: row["costcenter"])?.trim()?.ifBlank { null }
                val managerEmail = (row["responsable"] ?: row["manager"])?.trim()?.lowercase()?.ifBlank { null }
                validateUser(email, name, null, null)
                ensure(costCenter == null || costCenter.length <= 100) { msg("users.cost_center_too_long") }
                ensure(employeeNumber == null || employeeNumber.length <= 50) { msg("users.employee_number_too_long") }
                val manager =
                    managerEmail?.let {
                        users.findByTenantIdAndEmail(user.tenantId, it)
                            ?.takeIf { found -> found.deletedAt == null && found.role in setOf(UserRole.MANAGER, UserRole.ADMIN) }
                            ?: throw fr.codetics.ev_server.core.InvalidRequest(msg("users.manager_invalid"))
                    }
                val parsedRole =
                    runCatching { UserRole.valueOf(role) }.getOrNull()
                        ?: throw fr.codetics.ev_server.core.InvalidRequest(msg("users.import.unknown_role", role))
                ensure(badge.isEmpty() || badge.matches(Regex("[A-Za-z0-9_-]{1,20}"))) { msg("users.import.invalid_badge", badge) }
                db.atomic {
                    if (users.findByTenantIdAndEmail(user.tenantId, email) != null) {
                        skipped += 1
                        return@atomic
                    }
                    val account =
                        users.save(
                            UserEntity(
                                tenantId = user.tenantId,
                                email = email,
                                displayName = name,
                                role = parsedRole,
                                managerId = manager?.id,
                                costCenter = costCenter,
                                employeeNumber = employeeNumber,
                            )
                        ).also {
                            db.flush()
                        }
                    if (badge.isNotEmpty()) {
                        val key = ChargingTokenKey(user.tenantId, badge)
                        val token = chargingTokens.findById(key).orElse(null)
                        ensure(token == null || token.userId == null) { msg("users.import.badge_taken", badge) }
                        chargingTokens.save((token ?: ChargingTokenEntity(key)).apply { userId = account.id })
                    }
                    assignAutomaticSites(account)
                    invitations += account
                    created += 1
                }
            } catch (error: IllegalArgumentException) {
                errors += ImportError(line, error.message ?: msg("csv.invalid_line"))
            } catch (error: ResponseStatusException) {
                errors += ImportError(line, error.reason ?: msg("csv.invalid_line"))
            }
        }
        db.audit(user.tenantId, user.id.toString(), "USERS_IMPORT", "$created")
        invitations.forEach(::invite)
        return ImportResult(created = created, skipped = skipped, errors = errors)
    }

    fun sites(user: AppPrincipal, id: UUID): List<UserSiteResponse> {
        account(user.tenantId, id)
        val sites = organization.sites(user.tenantId).associateBy { it.id }
        return siteUsers.findAllByKeyTenantIdAndKeyUserId(user.tenantId, id).mapNotNull { assignment ->
            sites[assignment.key.siteId]?.let {
                UserSiteResponse(it.id, it.name, assignment.siteAdmin, assignment.siteOwner)
            }
        }.sortedBy { it.name }
    }

    fun assignSites(user: AppPrincipal, id: UUID, input: List<SiteAssignmentInput>): List<UserSiteResponse> {
        account(user.tenantId, id)
        val sites = organization.sites(user.tenantId).map { it.id }.toSet()
        ensure(input.all { it.siteId != null && it.siteId in sites }) { msg("users.site_missing") }
        db.atomic {
            siteUsers.deleteAll(siteUsers.findAllByKeyTenantIdAndKeyUserId(user.tenantId, id))
            db.flush()
            input.distinctBy { it.siteId }.forEach {
                siteUsers.save(SiteUserEntity(SiteUserKey(user.tenantId, requireNotNull(it.siteId), id), it.siteAdmin, it.siteOwner))
            }
            db.audit(user.tenantId, user.id.toString(), "USER_SITES", id.toString())
        }
        return sites(user, id)
    }

    fun siteUsers(user: AppPrincipal, siteId: UUID): List<SiteUserResponse> {
        ensure(organization.site(user.tenantId, siteId) != null) { msg("sites.not_found") }
        val assignments = siteUsers.findAllByKeyTenantIdAndKeySiteId(user.tenantId, siteId)
        val accounts = users.findAllByTenantIdAndIdIn(user.tenantId, assignments.map { it.key.userId }).associateBy { it.id }
        return assignments.mapNotNull { assignment ->
            accounts[assignment.key.userId]?.takeIf { it.deletedAt == null }?.let {
                SiteUserResponse(it.id, it.displayName, it.email, assignment.siteAdmin, assignment.siteOwner)
            }
        }.sortedBy { it.name }
    }

    fun assignUsers(user: AppPrincipal, siteId: UUID, input: List<SiteAssignmentInput>): List<SiteUserResponse> {
        ensure(organization.site(user.tenantId, siteId) != null) { msg("sites.not_found") }
        val ids = input.mapNotNull { it.userId }.toSet()
        ensure(ids.size == input.size) { msg("sites.user_once") }
        val known = users.findAllByTenantIdAndIdIn(user.tenantId, ids).filter { it.deletedAt == null }.map { it.id }.toSet()
        ensure(known.size == ids.size) { msg("sites.user_missing") }
        db.atomic {
            siteUsers.deleteAll(siteUsers.findAllByKeyTenantIdAndKeySiteId(user.tenantId, siteId))
            db.flush()
            input.forEach {
                siteUsers.save(SiteUserEntity(SiteUserKey(user.tenantId, siteId, requireNotNull(it.userId)), it.siteAdmin, it.siteOwner))
            }
            db.audit(user.tenantId, user.id.toString(), "SITE_USERS", siteId.toString())
        }
        return siteUsers(user, siteId)
    }

    fun preferences(tenant: UUID, id: UUID): NotificationPreferences {
        val account = account(tenant, id)
        return parsePreferences(account).normalized()
    }

    fun parsePreferences(account: UserEntity): NotificationPreferences =
        runCatching { db.json.readValue(account.notificationPreferences, NotificationPreferences::class.java) }
            .getOrDefault(NotificationPreferences())

    fun updatePreferences(tenant: UUID, id: UUID, input: NotificationPreferencesInput): NotificationPreferences =
        db.atomic {
            val account = account(tenant, id)
            account.notificationPreferences = db.encode(input.toPreferences())
            users.save(account)
            parsePreferences(account).normalized()
        }

    fun updateProfile(user: AppPrincipal, input: ProfileInput): UserResponse {
        validateUser(user.email, input.name, input.phone, input.locale)
        db.atomic {
            val account = account(user.tenantId, user.id)
            account.displayName = input.name.trim()
            account.phone = input.phone?.trim()?.ifBlank { null }
            (input.locale ?: I18n.requested())?.let { account.locale = it }
            users.save(account)
            db.audit(user.tenantId, user.id.toString(), "PROFILE_UPDATE", user.id.toString())
        }
        return find(user, user.id)
    }

    /** A manager must be an active MANAGER or ADMIN of the workspace, and reporting lines never loop. */
    private fun validateEmployment(tenant: UUID, id: UUID?, body: UserInput) {
        ensure(body.costCenter == null || body.costCenter.length <= 100) { msg("users.cost_center_too_long") }
        ensure(body.employeeNumber == null || body.employeeNumber.length <= 50) { msg("users.employee_number_too_long") }
        val managerId = body.managerId ?: return
        ensure(managerId != id) { msg("users.manager_self") }
        val manager = users.findByTenantIdAndId(tenant, managerId)?.takeIf { it.deletedAt == null && it.enabled }
        ensure(manager != null && manager.role in setOf(UserRole.MANAGER, UserRole.ADMIN)) { msg("users.manager_invalid") }
        var next = manager?.managerId
        var depth = 0
        while (next != null && depth < 50) {
            ensure(next != id) { msg("users.manager_cycle") }
            next = users.findByTenantIdAndId(tenant, next)?.managerId
            depth += 1
        }
    }

    private fun validateUser(email: String, name: String, phone: String?, locale: String?) {
        validateEmail(email)
        ensure(name.isNotBlank() && name.length <= 200) { msg("validation.name_length") }
        ensure(phone == null || phone.isBlank() || phone.matches(Regex("[+0-9 ().-]{4,40}"))) { msg("validation.phone") }
        ensure(locale == null || locale in LOCALES) { msg("users.unsupported_language") }
    }

    private companion object {
        val LOCALES = I18n.LANGUAGES
    }
}