package net.siegeln.cameleer.saas.identity;

import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LogtoManagementClient {

    private static final Logger log = LoggerFactory.getLogger(LogtoManagementClient.class);

    private final LogtoConfig config;
    private final RestClient restClient;

    private String cachedToken;
    private Instant tokenExpiry = Instant.MIN;

    public LogtoManagementClient(LogtoConfig config) {
        this.config = config;
        this.restClient = RestClient.builder()
                .defaultHeader("Content-Type", "application/json")
                .build();
    }

    public boolean isAvailable() {
        return config.isConfigured();
    }

    public String createOrganization(String name, String description) {
        if (!isAvailable()) {
            log.warn("Logto not configured — skipping organization creation for '{}'", name);
            return null;
        }

        var body = Map.of("name", name, "description", description != null ? description : "");

        var response = restClient.post()
                .uri(config.getLogtoEndpoint() + "/api/organizations")
                .header("Authorization", "Bearer " + getAccessToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(body)
                .retrieve()
                .body(JsonNode.class);

        return response != null ? response.get("id").asText() : null;
    }

    public void addUserToOrganization(String orgId, String userId) {
        if (!isAvailable()) return;

        restClient.post()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId + "/users")
                .header("Authorization", "Bearer " + getAccessToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(Map.of("userIds", new String[]{userId}))
                .retrieve()
                .toBodilessEntity();
    }

    public void deleteOrganization(String orgId) {
        if (!isAvailable()) return;

        restClient.delete()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId)
                .header("Authorization", "Bearer " + getAccessToken())
                .retrieve()
                .toBodilessEntity();
    }

    public List<Map<String, String>> getUserOrganizations(String userId) {
        if (!isAvailable()) return List.of();

        try {
            var response = restClient.get()
                    .uri(config.getLogtoEndpoint() + "/api/users/" + userId + "/organizations")
                    .header("Authorization", "Bearer " + getAccessToken())
                    .retrieve()
                    .body(JsonNode.class);

            List<Map<String, String>> orgs = new ArrayList<>();
            if (response != null && response.isArray()) {
                for (var node : response) {
                    orgs.add(Map.of(
                            "id", node.get("id").asText(),
                            "name", node.get("name").asText()
                    ));
                }
            }
            return orgs;
        } catch (Exception e) {
            log.warn("Failed to get user organizations for {}: {}", userId, e.getMessage());
            return List.of();
        }
    }

    /** List members of a Logto organization. */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> listOrganizationMembers(String orgId) {
        if (!isAvailable()) return List.of();
        try {
            var resp = restClient.get()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId + "/users")
                .header("Authorization", "Bearer " + getAccessToken())
                .retrieve()
                .body(List.class);
            return resp != null ? resp : List.of();
        } catch (Exception e) {
            log.warn("Failed to list org members for {}: {}", orgId, e.getMessage());
            return List.of();
        }
    }

    /** Get roles assigned to a user within an organization. */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getUserOrganizationRoles(String orgId, String userId) {
        if (!isAvailable()) return List.of();
        try {
            var resp = restClient.get()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId + "/users/" + userId + "/roles")
                .header("Authorization", "Bearer " + getAccessToken())
                .retrieve()
                .body(List.class);
            return resp != null ? resp : List.of();
        } catch (Exception e) {
            log.warn("Failed to get user roles: {}", e.getMessage());
            return List.of();
        }
    }

    /** Assign a role to a user in an organization. */
    public void assignOrganizationRole(String orgId, String userId, String roleId) {
        if (!isAvailable()) return;
        try {
            restClient.post()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId + "/users/" + userId + "/roles")
                .header("Authorization", "Bearer " + getAccessToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(Map.of("organizationRoleIds", List.of(roleId)))
                .retrieve()
                .toBodilessEntity();
        } catch (Exception e) {
            log.warn("Failed to assign role: {}", e.getMessage());
        }
    }

    /** Remove a user from an organization. */
    public void removeUserFromOrganization(String orgId, String userId) {
        if (!isAvailable()) return;
        try {
            restClient.delete()
                .uri(config.getLogtoEndpoint() + "/api/organizations/" + orgId + "/users/" + userId)
                .header("Authorization", "Bearer " + getAccessToken())
                .retrieve()
                .toBodilessEntity();
        } catch (Exception e) {
            log.warn("Failed to remove user from org: {}", e.getMessage());
        }
    }

    /** Create a user in Logto and add to organization with role. */
    @SuppressWarnings("unchecked")
    public String createAndInviteUser(String email, String orgId, String roleId) {
        if (!isAvailable()) return null;
        try {
            var userResp = (Map<String, Object>) restClient.post()
                .uri(config.getLogtoEndpoint() + "/api/users")
                .header("Authorization", "Bearer " + getAccessToken())
                .contentType(MediaType.APPLICATION_JSON)
                .body(Map.of("primaryEmail", email, "name", email.split("@")[0]))
                .retrieve()
                .body(Map.class);
            String userId = String.valueOf(userResp.get("id"));
            addUserToOrganization(orgId, userId);
            if (roleId != null) {
                assignOrganizationRole(orgId, userId, roleId);
            }
            return userId;
        } catch (Exception e) {
            log.error("Failed to create and invite user: {}", e.getMessage());
            throw new RuntimeException("Invite failed: " + e.getMessage(), e);
        }
    }

    /** List available organization roles. */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> listOrganizationRoles() {
        if (!isAvailable()) return List.of();
        try {
            var resp = restClient.get()
                .uri(config.getLogtoEndpoint() + "/api/organization-roles")
                .header("Authorization", "Bearer " + getAccessToken())
                .retrieve()
                .body(List.class);
            return resp != null ? resp : List.of();
        } catch (Exception e) {
            log.warn("Failed to list org roles: {}", e.getMessage());
            return List.of();
        }
    }

    private static final String MGMT_API_RESOURCE = "https://default.logto.app/api";

    private synchronized String getAccessToken() {
        if (cachedToken != null && Instant.now().isBefore(tokenExpiry.minusSeconds(60))) {
            return cachedToken;
        }

        try {
            var response = restClient.post()
                    .uri(config.getLogtoEndpoint() + "/oidc/token")
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    .body("grant_type=client_credentials"
                            + "&client_id=" + config.getM2mClientId()
                            + "&client_secret=" + config.getM2mClientSecret()
                            + "&resource=" + MGMT_API_RESOURCE
                            + "&scope=all")
                    .retrieve()
                    .body(JsonNode.class);

            cachedToken = response.get("access_token").asText();
            long expiresIn = response.get("expires_in").asLong();
            tokenExpiry = Instant.now().plusSeconds(expiresIn);

            return cachedToken;
        } catch (Exception e) {
            log.error("Failed to get Logto Management API token", e);
            throw new RuntimeException("Logto authentication failed", e);
        }
    }
}
