/**
 * Tests for API Schemas
 * FE-TYPE-002: Test Zod schema validation for API responses
 */

import { describe, it, expect } from 'vitest';
import {
  userSchema,
  trackSchema,
  playlistSchema,
  conversationSchema,
  messageSchema,
  validateApiResponse,
  safeValidateApiResponse,
  validateApiResponseArray,
  validatePaginatedResponse,
} from './apiSchemas';

describe('apiSchemas', () => {
  describe('userSchema', () => {
    it('should validate valid user', () => {
      const validUser = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = userSchema.safeParse(validUser);
      expect(result.success).toBe(true);
    });

    it('should reject invalid UUID', () => {
      const invalidUser = {
        id: 'not-a-uuid',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = userSchema.safeParse(invalidUser);
      expect(result.success).toBe(false);
    });

    it('should reject invalid email', () => {
      const invalidUser = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        username: 'testuser',
        email: 'not-an-email',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = userSchema.safeParse(invalidUser);
      expect(result.success).toBe(false);
    });
  });

  describe('trackSchema', () => {
    it('should validate valid track', () => {
      const validTrack = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        creator_id: '123e4567-e89b-12d3-a456-426614174001',
        title: 'Test Track',
        artist: 'Test Artist',
        album: 'Test Album',
        duration: 180,
        genre: 'Rock',
        year: 2024,
        file_path: '/path/to/file.mp3',
        file_size: 5000000,
        format: 'mp3',
        bitrate: 320,
        sample_rate: 44100,
        is_public: true,
        status: 'completed',
        stream_status: 'ready',
        play_count: 0,
        like_count: 0,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = trackSchema.safeParse(validTrack);
      expect(result.success).toBe(true);
    });

    it('should reject negative duration', () => {
      const invalidTrack = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        creator_id: '123e4567-e89b-12d3-a456-426614174001',
        title: 'Test Track',
        artist: 'Test Artist',
        album: 'Test Album',
        duration: -10,
        genre: 'Rock',
        year: 2024,
        file_path: '/path/to/file.mp3',
        file_size: 5000000,
        format: 'mp3',
        bitrate: 320,
        sample_rate: 44100,
        is_public: true,
        status: 'completed',
        stream_status: 'ready',
        play_count: 0,
        like_count: 0,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = trackSchema.safeParse(invalidTrack);
      expect(result.success).toBe(false);
    });
  });

  describe('playlistSchema', () => {
    it('should validate valid playlist', () => {
      const validPlaylist = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        user_id: '123e4567-e89b-12d3-a456-426614174001',
        title: 'Test Playlist',
        is_public: true,
        track_count: 10,
        follower_count: 5,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = playlistSchema.safeParse(validPlaylist);
      expect(result.success).toBe(true);
    });
  });

  describe('conversationSchema', () => {
    it('should validate valid conversation', () => {
      const validConversation = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        name: 'Test Conversation',
        type: 'group',
        creator_id: '123e4567-e89b-12d3-a456-426614174001',
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = conversationSchema.safeParse(validConversation);
      expect(result.success).toBe(true);
    });
  });

  describe('validateApiResponse', () => {
    it('should validate and return data', () => {
      const validUser = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = validateApiResponse(userSchema, validUser);
      expect(result.id).toBe(validUser.id);
      expect(result.username).toBe(validUser.username);
    });

    it('should throw on invalid data', () => {
      const invalidUser = {
        id: 'not-a-uuid',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      expect(() => validateApiResponse(userSchema, invalidUser)).toThrow();
    });
  });

  describe('safeValidateApiResponse', () => {
    it('should return success for valid data', () => {
      const validUser = {
        id: '123e4567-e89b-12d3-a456-426614174000',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = safeValidateApiResponse(userSchema, validUser);
      expect(result.success).toBe(true);
      expect(result.data).toBeDefined();
    });

    it('should return error for invalid data', () => {
      const invalidUser = {
        id: 'not-a-uuid',
        username: 'testuser',
        email: 'test@example.com',
        role: 'user',
        is_active: true,
        is_verified: true,
        is_admin: false,
        is_public: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z',
      };

      const result = safeValidateApiResponse(userSchema, invalidUser);
      expect(result.success).toBe(false);
      expect(result.error).toBeDefined();
    });
  });

  describe('validateApiResponseArray', () => {
    it('should validate array of items', () => {
      const validUsers = [
        {
          id: '123e4567-e89b-12d3-a456-426614174000',
          username: 'user1',
          email: 'user1@example.com',
          role: 'user',
          is_active: true,
          is_verified: true,
          is_admin: false,
          is_public: true,
          created_at: '2024-01-01T00:00:00Z',
          updated_at: '2024-01-01T00:00:00Z',
        },
        {
          id: '123e4567-e89b-12d3-a456-426614174001',
          username: 'user2',
          email: 'user2@example.com',
          role: 'user',
          is_active: true,
          is_verified: true,
          is_admin: false,
          is_public: true,
          created_at: '2024-01-01T00:00:00Z',
          updated_at: '2024-01-01T00:00:00Z',
        },
      ];

      const result = validateApiResponseArray(userSchema, validUsers);
      expect(result.length).toBe(2);
      expect(result[0].username).toBe('user1');
    });
  });

  describe('validatePaginatedResponse', () => {
    it('should validate paginated response', () => {
      const validPaginated = {
        items: [
          {
            id: '123e4567-e89b-12d3-a456-426614174000',
            username: 'user1',
            email: 'user1@example.com',
            role: 'user',
            is_active: true,
            is_verified: true,
            is_admin: false,
            is_public: true,
            created_at: '2024-01-01T00:00:00Z',
            updated_at: '2024-01-01T00:00:00Z',
          },
        ],
        pagination: {
          page: 1,
          limit: 20,
          total: 1,
          total_pages: 1,
          has_next: false,
          has_prev: false,
        },
      };

      const result = validatePaginatedResponse(userSchema, validPaginated);
      expect(result.items.length).toBe(1);
      expect(result.pagination.page).toBe(1);
    });
  });
});
