/**
 * Moderation Queue & Workflow Integration Tests
 * 
 * Tests for moderation queue operations, locking, and state management.
 */

import { supabase } from '@/integrations/supabase/client';
import type { TestSuite, TestResult } from '../testRunner';

export const moderationTestSuite: TestSuite = {
  id: 'moderation',
  name: 'Moderation Queue & Workflow',
  description: 'Tests for moderation queue operations and submission workflows',
  tests: [
    {
      id: 'moderation-001',
      name: 'Moderation Functions Available',
      description: 'Validates moderation database functions are accessible',
      run: async (): Promise<TestResult> => {
        const startTime = Date.now();

        try {
          const { data: userData } = await supabase.auth.getUser();
          if (!userData.user) throw new Error('No authenticated user');

          // Test that moderation functions exist and are callable
          const { data: isMod, error: modError } = await supabase
            .rpc('is_moderator', {
              _user_id: userData.user.id
            });

          if (modError) throw new Error(`is_moderator function failed: ${modError.message}`);

          const duration = Date.now() - startTime;

          return {
            id: 'moderation-001',
            name: 'Moderation Functions Available',
            suite: 'Moderation Queue & Workflow',
            status: 'pass',
            duration,
            timestamp: new Date().toISOString(),
            details: {
              isModerator: isMod,
              functionsAccessible: true
            }
          };

        } catch (error) {
          return {
            id: 'moderation-001',
            name: 'Moderation Functions Available',
            suite: 'Moderation Queue & Workflow',
            status: 'fail',
            duration: Date.now() - startTime,
            error: error instanceof Error ? error.message : String(error),
            timestamp: new Date().toISOString()
          };
        }
      }
    }
  ]
};
