import { describe, it, expect, vi, beforeEach } from 'vitest';

// Mock apiClient
vi.mock('../client', () => ({
  default: {
    get: vi.fn(),
    post: vi.fn(),
  },
}));

import { getPlatformOAuthSettings, updatePlatformOAuthSettings } from '../platformOAuth';
import apiClient from '../client';
import type { PlatformOAuthSettings, PlatformOAuthSettingsUpdate } from '../platformOAuth';

describe('platformOAuth API', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('getPlatformOAuthSettings', () => {
    it('fetches platform OAuth settings with all providers', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'google-client-id-123',
          client_secret: 'google-secret-456',
        },
        apple: {
          enabled: true,
          client_id: 'apple-client-id-789',
          client_secret: 'apple-secret-012',
          team_id: 'APPLE-TEAM-ID',
          key_id: 'APPLE-KEY-ID',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: true,
          client_id: 'microsoft-client-id-345',
          client_secret: 'microsoft-secret-678',
          tenant_id: 'common',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(apiClient.get).toHaveBeenCalledWith('/platform/settings/oauth/');
      expect(result).toEqual(mockSettings);
    });

    it('fetches settings with registration disabled', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: false,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(result.oauth_allow_registration).toBe(false);
      expect(result.google.enabled).toBe(false);
      expect(result.microsoft.enabled).toBe(false);
    });

    it('fetches settings with Apple-specific fields', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: true,
          client_id: 'com.example.app',
          client_secret: 'apple-private-key',
          team_id: 'TEAM123456',
          key_id: 'KEY987654',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(result.apple.enabled).toBe(true);
      expect(result.apple.team_id).toBe('TEAM123456');
      expect(result.apple.key_id).toBe('KEY987654');
    });

    it('fetches settings with Microsoft tenant ID', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: true,
          client_id: 'ms-client-123',
          client_secret: 'ms-secret-456',
          tenant_id: 'organizations',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(result.microsoft.enabled).toBe(true);
      expect(result.microsoft.tenant_id).toBe('organizations');
    });

    it('fetches settings with multiple providers enabled', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'google-123',
          client_secret: 'google-secret',
        },
        apple: {
          enabled: true,
          client_id: 'apple-456',
          client_secret: 'apple-secret',
          team_id: 'TEAM',
          key_id: 'KEY',
        },
        facebook: {
          enabled: true,
          client_id: 'fb-789',
          client_secret: 'fb-secret',
        },
        linkedin: {
          enabled: true,
          client_id: 'li-012',
          client_secret: 'li-secret',
        },
        microsoft: {
          enabled: true,
          client_id: 'ms-345',
          client_secret: 'ms-secret',
          tenant_id: 'common',
        },
        twitter: {
          enabled: true,
          client_id: 'tw-678',
          client_secret: 'tw-secret',
        },
        twitch: {
          enabled: true,
          client_id: 'twitch-901',
          client_secret: 'twitch-secret',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(result.google.enabled).toBe(true);
      expect(result.apple.enabled).toBe(true);
      expect(result.facebook.enabled).toBe(true);
      expect(result.linkedin.enabled).toBe(true);
      expect(result.microsoft.enabled).toBe(true);
      expect(result.twitter.enabled).toBe(true);
      expect(result.twitch.enabled).toBe(true);
    });

    it('returns data from response.data', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'test-123',
          client_secret: 'test-secret',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      const result = await getPlatformOAuthSettings();

      expect(result).toEqual(mockSettings);
      expect(result).toHaveProperty('oauth_allow_registration');
      expect(result).toHaveProperty('google');
      expect(result).toHaveProperty('apple');
      expect(result).toHaveProperty('facebook');
      expect(result).toHaveProperty('linkedin');
      expect(result).toHaveProperty('microsoft');
      expect(result).toHaveProperty('twitter');
      expect(result).toHaveProperty('twitch');
    });
  });

  describe('updatePlatformOAuthSettings', () => {
    it('updates global registration setting', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_allow_registration: false,
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: false,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result).toEqual(mockResponse);
      expect(result.oauth_allow_registration).toBe(false);
    });

    it('updates Google OAuth settings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: true,
        oauth_google_client_id: 'new-google-client-id',
        oauth_google_client_secret: 'new-google-secret',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'new-google-client-id',
          client_secret: 'new-google-secret',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.google.enabled).toBe(true);
      expect(result.google.client_id).toBe('new-google-client-id');
      expect(result.google.client_secret).toBe('new-google-secret');
    });

    it('updates Apple OAuth settings with all fields', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_apple_enabled: true,
        oauth_apple_client_id: 'com.myapp.service',
        oauth_apple_client_secret: 'apple-private-key-pem',
        oauth_apple_team_id: 'APPLETEAM123',
        oauth_apple_key_id: 'APPLEKEY456',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: true,
          client_id: 'com.myapp.service',
          client_secret: 'apple-private-key-pem',
          team_id: 'APPLETEAM123',
          key_id: 'APPLEKEY456',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.apple.enabled).toBe(true);
      expect(result.apple.team_id).toBe('APPLETEAM123');
      expect(result.apple.key_id).toBe('APPLEKEY456');
    });

    it('updates Facebook OAuth settings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_facebook_enabled: true,
        oauth_facebook_client_id: 'fb-app-id-789',
        oauth_facebook_client_secret: 'fb-app-secret-012',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: true,
          client_id: 'fb-app-id-789',
          client_secret: 'fb-app-secret-012',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.facebook.enabled).toBe(true);
      expect(result.facebook.client_id).toBe('fb-app-id-789');
    });

    it('updates LinkedIn OAuth settings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_linkedin_enabled: true,
        oauth_linkedin_client_id: 'li-client-345',
        oauth_linkedin_client_secret: 'li-secret-678',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: true,
          client_id: 'li-client-345',
          client_secret: 'li-secret-678',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.linkedin.enabled).toBe(true);
      expect(result.linkedin.client_id).toBe('li-client-345');
    });

    it('updates Microsoft OAuth settings with tenant ID', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_microsoft_enabled: true,
        oauth_microsoft_client_id: 'ms-app-id-901',
        oauth_microsoft_client_secret: 'ms-app-secret-234',
        oauth_microsoft_tenant_id: 'organizations',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: true,
          client_id: 'ms-app-id-901',
          client_secret: 'ms-app-secret-234',
          tenant_id: 'organizations',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.microsoft.enabled).toBe(true);
      expect(result.microsoft.tenant_id).toBe('organizations');
    });

    it('updates Twitter OAuth settings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_twitter_enabled: true,
        oauth_twitter_client_id: 'twitter-client-567',
        oauth_twitter_client_secret: 'twitter-secret-890',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: true,
          client_id: 'twitter-client-567',
          client_secret: 'twitter-secret-890',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.twitter.enabled).toBe(true);
      expect(result.twitter.client_id).toBe('twitter-client-567');
    });

    it('updates Twitch OAuth settings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_twitch_enabled: true,
        oauth_twitch_client_id: 'twitch-client-123',
        oauth_twitch_client_secret: 'twitch-secret-456',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: true,
          client_id: 'twitch-client-123',
          client_secret: 'twitch-secret-456',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.twitch.enabled).toBe(true);
      expect(result.twitch.client_id).toBe('twitch-client-123');
    });

    it('updates multiple providers at once', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_allow_registration: true,
        oauth_google_enabled: true,
        oauth_google_client_id: 'google-123',
        oauth_google_client_secret: 'google-secret',
        oauth_microsoft_enabled: true,
        oauth_microsoft_client_id: 'ms-456',
        oauth_microsoft_client_secret: 'ms-secret',
        oauth_microsoft_tenant_id: 'common',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'google-123',
          client_secret: 'google-secret',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: true,
          client_id: 'ms-456',
          client_secret: 'ms-secret',
          tenant_id: 'common',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.google.enabled).toBe(true);
      expect(result.microsoft.enabled).toBe(true);
    });

    it('disables a provider by setting enabled to false', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: false,
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: 'google-client-id',
          client_secret: 'google-secret',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.google.enabled).toBe(false);
    });

    it('sends partial update with only changed fields', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_client_id: 'updated-client-id',
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: 'updated-client-id',
          client_secret: 'existing-secret',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
      expect(result.google.client_id).toBe('updated-client-id');
    });

    it('returns updated settings from response.data', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_allow_registration: true,
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      const result = await updatePlatformOAuthSettings(updateData);

      expect(result).toEqual(mockResponse);
      expect(result).toHaveProperty('oauth_allow_registration');
      expect(result).toHaveProperty('google');
      expect(result).toHaveProperty('apple');
      expect(result).toHaveProperty('facebook');
      expect(result).toHaveProperty('linkedin');
      expect(result).toHaveProperty('microsoft');
      expect(result).toHaveProperty('twitter');
      expect(result).toHaveProperty('twitch');
    });
  });

  describe('error handling', () => {
    it('propagates errors from getPlatformOAuthSettings', async () => {
      const error = new Error('Network error');
      vi.mocked(apiClient.get).mockRejectedValue(error);

      await expect(getPlatformOAuthSettings()).rejects.toThrow('Network error');
    });

    it('propagates errors from updatePlatformOAuthSettings', async () => {
      const error = new Error('Unauthorized');
      vi.mocked(apiClient.post).mockRejectedValue(error);

      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: true,
      };

      await expect(updatePlatformOAuthSettings(updateData)).rejects.toThrow('Unauthorized');
    });

    it('handles validation errors from backend', async () => {
      const error = new Error('Invalid client ID format');
      vi.mocked(apiClient.post).mockRejectedValue(error);

      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_client_id: 'invalid-id',
      };

      await expect(updatePlatformOAuthSettings(updateData)).rejects.toThrow('Invalid client ID format');
    });

    it('handles permission errors', async () => {
      const error = new Error('Permission denied');
      vi.mocked(apiClient.post).mockRejectedValue(error);

      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: true,
      };

      await expect(updatePlatformOAuthSettings(updateData)).rejects.toThrow('Permission denied');
    });

    it('handles server errors on GET request', async () => {
      const error = new Error('Internal server error');
      vi.mocked(apiClient.get).mockRejectedValue(error);

      await expect(getPlatformOAuthSettings()).rejects.toThrow('Internal server error');
    });

    it('handles server errors on POST request', async () => {
      const error = new Error('Internal server error');
      vi.mocked(apiClient.post).mockRejectedValue(error);

      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_microsoft_enabled: true,
      };

      await expect(updatePlatformOAuthSettings(updateData)).rejects.toThrow('Internal server error');
    });
  });

  describe('endpoint validation', () => {
    it('calls correct GET endpoint for getPlatformOAuthSettings', async () => {
      const mockSettings: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.get).mockResolvedValue({ data: mockSettings });

      await getPlatformOAuthSettings();

      expect(apiClient.get).toHaveBeenCalledTimes(1);
      expect(apiClient.get).toHaveBeenCalledWith('/platform/settings/oauth/');
    });

    it('calls correct POST endpoint for updatePlatformOAuthSettings', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: true,
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      await updatePlatformOAuthSettings(updateData);

      expect(apiClient.post).toHaveBeenCalledTimes(1);
      expect(apiClient.post).toHaveBeenCalledWith('/platform/settings/oauth/', updateData);
    });

    it('uses POST method for updates (not PUT or PATCH)', async () => {
      const updateData: PlatformOAuthSettingsUpdate = {
        oauth_google_enabled: true,
      };

      const mockResponse: PlatformOAuthSettings = {
        oauth_allow_registration: true,
        google: {
          enabled: true,
          client_id: '',
          client_secret: '',
        },
        apple: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        facebook: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        linkedin: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        microsoft: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitter: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
        twitch: {
          enabled: false,
          client_id: '',
          client_secret: '',
        },
      };

      vi.mocked(apiClient.post).mockResolvedValue({ data: mockResponse });

      await updatePlatformOAuthSettings(updateData);

      // Verify POST was called, not PUT or PATCH
      expect(apiClient.post).toHaveBeenCalled();
    });
  });
});
