package com.magistr.app.controller;

import com.magistr.app.config.auth.AuthContext;
import com.magistr.app.config.auth.AuthenticatedUser;
import com.magistr.app.model.Role;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class AuthControllerTest {

    @AfterEach
    void clearAuthContext() {
        AuthContext.clear();
    }

    @Test
    void meAllowsNullDepartmentId() {
        AuthController controller = new AuthController(null, null, null, null, null);
        AuthContext.setCurrentUser(new AuthenticatedUser(10L, "admin", Role.ADMIN, null));

        ResponseEntity<?> response = controller.me();

        assertThat(response.getStatusCode().value()).isEqualTo(200);
        assertThat(response.getBody()).isInstanceOf(Map.class);
        @SuppressWarnings("unchecked")
        Map<String, Object> body = (Map<String, Object>) response.getBody();
        assertThat(body).containsEntry("userId", 10L);
        assertThat(body).containsEntry("username", "admin");
        assertThat(body).containsEntry("role", "ADMIN");
        assertThat(body).containsKey("departmentId");
        assertThat(body.get("departmentId")).isNull();
    }
}
