/**
 * API Service Tests
 *
 * Tests the API service module structure and exported functions.
 * API interceptor behavior is tested in integration with AuthContext.
 */

import {
  authAPI,
  userAPI,
  itemAPI,
  rentalAPI,
  messageAPI,
  mapsAPI,
  stripeAPI,
  addressAPI,
  conditionCheckAPI,
  forumAPI,
  feedbackAPI,
  fetchCSRFToken,
  resetCSRFToken,
} from '../../services/api';
import api from '../../services/api';

describe('API Service', () => {
  describe('Module Exports', () => {
    it('exports authAPI with correct methods', () => {
      expect(authAPI).toBeDefined();
      expect(typeof authAPI.login).toBe('function');
      expect(typeof authAPI.register).toBe('function');
      expect(typeof authAPI.logout).toBe('function');
      expect(typeof authAPI.googleLogin).toBe('function');
      expect(typeof authAPI.getStatus).toBe('function');
      expect(typeof authAPI.verifyEmail).toBe('function');
      expect(typeof authAPI.forgotPassword).toBe('function');
      expect(typeof authAPI.resetPassword).toBe('function');
    });

    it('exports userAPI with correct methods', () => {
      expect(userAPI).toBeDefined();
      expect(typeof userAPI.getProfile).toBe('function');
      expect(typeof userAPI.updateProfile).toBe('function');
      expect(typeof userAPI.uploadProfileImage).toBe('function');
    });

    it('exports itemAPI with correct methods', () => {
      expect(itemAPI).toBeDefined();
      expect(typeof itemAPI.getItems).toBe('function');
      expect(typeof itemAPI.getItem).toBe('function');
      expect(typeof itemAPI.createItem).toBe('function');
      expect(typeof itemAPI.updateItem).toBe('function');
      expect(typeof itemAPI.deleteItem).toBe('function');
    });

    it('exports rentalAPI with correct methods', () => {
      expect(rentalAPI).toBeDefined();
      expect(typeof rentalAPI.createRental).toBe('function');
      expect(typeof rentalAPI.getRentals).toBe('function');
      expect(typeof rentalAPI.getListings).toBe('function');
      expect(typeof rentalAPI.updateRentalStatus).toBe('function');
      expect(typeof rentalAPI.cancelRental).toBe('function');
    });

    it('exports messageAPI with correct methods', () => {
      expect(messageAPI).toBeDefined();
      expect(typeof messageAPI.getMessages).toBe('function');
      expect(typeof messageAPI.getConversations).toBe('function');
      expect(typeof messageAPI.sendMessage).toBe('function');
      expect(typeof messageAPI.getUnreadCount).toBe('function');
    });

    it('exports mapsAPI with correct methods', () => {
      expect(mapsAPI).toBeDefined();
      expect(typeof mapsAPI.placesAutocomplete).toBe('function');
      expect(typeof mapsAPI.placeDetails).toBe('function');
      expect(typeof mapsAPI.geocode).toBe('function');
    });

    it('exports stripeAPI with correct methods', () => {
      expect(stripeAPI).toBeDefined();
      expect(typeof stripeAPI.getCheckoutSession).toBe('function');
      expect(typeof stripeAPI.createConnectedAccount).toBe('function');
      expect(typeof stripeAPI.createAccountLink).toBe('function');
      expect(typeof stripeAPI.getAccountStatus).toBe('function');
    });

    it('exports CSRF token management functions', () => {
      expect(typeof fetchCSRFToken).toBe('function');
      expect(typeof resetCSRFToken).toBe('function');
    });

  });

  describe('CSRF Token Management', () => {
    it('resetCSRFToken clears the token', () => {
      // Should not throw
      expect(() => resetCSRFToken()).not.toThrow();
    });
  });

  describe('API Configuration', () => {
    it('creates axios instance with correct base URL', () => {
      expect(api).toBeDefined();
      expect(api.defaults).toBeDefined();
    });
  });
});

describe('API Namespaces', () => {
  describe('authAPI', () => {
    it('has all authentication methods', () => {
      const expectedMethods = [
        'register',
        'login',
        'googleLogin',
        'logout',
        'refresh',
        'getCSRFToken',
        'getStatus',
        'verifyEmail',
        'resendVerification',
        'forgotPassword',
        'verifyResetToken',
        'resetPassword',
      ];

      expectedMethods.forEach((method) => {
        expect((authAPI as any)[method]).toBeDefined();
        expect(typeof (authAPI as any)[method]).toBe('function');
      });
    });
  });

  describe('addressAPI', () => {
    it('has all address management methods', () => {
      const expectedMethods = [
        'getAddresses',
        'createAddress',
        'updateAddress',
        'deleteAddress',
      ];

      expectedMethods.forEach((method) => {
        expect((addressAPI as any)[method]).toBeDefined();
        expect(typeof (addressAPI as any)[method]).toBe('function');
      });
    });
  });

  describe('conditionCheckAPI', () => {
    it('has all condition check methods', () => {
      const expectedMethods = [
        'submitConditionCheck',
        'getBatchConditionChecks',
        'getAvailableChecks',
      ];

      expectedMethods.forEach((method) => {
        expect((conditionCheckAPI as any)[method]).toBeDefined();
        expect(typeof (conditionCheckAPI as any)[method]).toBe('function');
      });
    });
  });

  describe('forumAPI', () => {
    it('has all forum methods', () => {
      const expectedMethods = [
        'getPosts',
        'getPost',
        'createPost',
        'updatePost',
        'deletePost',
        'createComment',
        'updateComment',
        'deleteComment',
        'getTags',
      ];

      expectedMethods.forEach((method) => {
        expect((forumAPI as any)[method]).toBeDefined();
        expect(typeof (forumAPI as any)[method]).toBe('function');
      });
    });
  });

  describe('feedbackAPI', () => {
    it('has feedback submission method', () => {
      expect(feedbackAPI.submitFeedback).toBeDefined();
      expect(typeof feedbackAPI.submitFeedback).toBe('function');
    });
  });
});
