import pytest
import requests
import json
import os

BASE_URL = "http://localhost:1111"
LOGIN_ENDPOINT = f"{BASE_URL}/authentication/login"

# Load test data
current_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(current_dir, 'test_data.json'), 'r') as f:
    TEST_DATA = json.load(f)

@pytest.fixture
def test_credentials():
    return TEST_DATA['test_credentials']

@pytest.fixture
def headers():
    return {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

class TestLogin:
    def test_successful_login(self, test_credentials, headers):
        """Test successful login with provided credentials"""
        response = requests.post(LOGIN_ENDPOINT, json=test_credentials, headers=headers)
        assert response.status_code == 200, f"Login failed with status {response.status_code}. Response: {response.text}"
        data = response.json()
        assert "token" in data, f"Token not found in response. Response: {data}"
        assert data.get("status") == "success", f"Status is not success. Response: {data}"

    def test_invalid_credentials(self, headers):
        """Test login with invalid credentials"""
        invalid_credentials = {
            "domain": "evyos.com.tr",
            "access_key": "invalid@evyos.com.tr",
            "password": "wrongpassword",
            "remember_me": False
        }
        response = requests.post(LOGIN_ENDPOINT, json=invalid_credentials, headers=headers)
        assert response.status_code in [401, 403], f"Expected 401 or 403, got {response.status_code}. Response: {response.text}"

    def test_missing_fields(self, headers):
        """Test login with missing required fields"""
        incomplete_credentials = {
            "domain": "evyos.com.tr",
            "access_key": "test@evyos.com.tr"
            # missing password
        }
        response = requests.post(LOGIN_ENDPOINT, json=incomplete_credentials, headers=headers)
        assert response.status_code == 422, f"Expected 422, got {response.status_code}. Response: {response.text}"

    def test_invalid_domain(self, headers):
        """Test login with invalid domain"""
        invalid_domain_credentials = {
            "domain": "invalid-domain.com",
            "access_key": "test@evyos.com.tr",
            "password": "string",
            "remember_me": False
        }
        response = requests.post(LOGIN_ENDPOINT, json=invalid_domain_credentials, headers=headers)
        assert response.status_code in [400, 401], f"Expected 400 or 401, got {response.status_code}. Response: {response.text}"

    def test_malformed_json(self, headers):
        """Test login with malformed JSON"""
        response = requests.post(LOGIN_ENDPOINT, data="invalid json", headers=headers)
        assert response.status_code == 422, f"Expected 422, got {response.status_code}. Response: {response.text}"
        
    def test_empty_request(self, headers):
        """Test login with empty request body"""
        response = requests.post(LOGIN_ENDPOINT, json={}, headers=headers)
        assert response.status_code == 422, f"Expected 422, got {response.status_code}. Response: {response.text}"
