/**
 * Realistic API response fixtures for testing.
 * Based on actual FCP API response structures.
 */

export const fixtures = {
  // Spot availability responses
  availability: {
    withInstances: {
      auctions: [
        {
          id: 'auction_a100_west',
          instance_type: 'gpu.a100.1x',
          region: 'us-west-2',
          gpu_count: 1,
          price: 25.60,
          quantity: 5,
          gpu_memory_gb: 40,
          cpu_count: 12,
          memory_gb: 85
        },
        {
          id: 'auction_h100_east',
          instance_type: 'gpu.h100.2x',
          region: 'us-east-1',
          gpu_count: 2,
          price: 51.20,
          quantity: 3,
          gpu_memory_gb: 80,
          cpu_count: 24,
          memory_gb: 170
        },
        {
          id: 'auction_a100_east',
          instance_type: 'gpu.a100.1x',
          region: 'us-east-1',
          gpu_count: 1,
          price: 26.40,
          quantity: 8,
          gpu_memory_gb: 40,
          cpu_count: 12,
          memory_gb: 85
        }
      ]
    },
    empty: {
      auctions: []
    },
    limited: {
      auctions: [
        {
          id: 'auction_h100_limited',
          instance_type: 'gpu.h100.8x',
          region: 'us-west-2',
          gpu_count: 8,
          price: 204.80,
          quantity: 1
        }
      ]
    }
  },

  // SSH key responses
  sshKeys: {
    empty: {
      ssh_keys: []
    },
    withKeys: {
      ssh_keys: [
        {
          id: 'key_existing',
          name: 'old-mithril-key',
          public_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTest1234567890abcdefghijklmnopqrstuvwxyz test@mithril',
          created_at: '2024-01-01T00:00:00Z'
        },
        {
          id: 'key_other',
          name: 'deployment-key',
          public_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9999999999999999999999999999999999999999 deploy@system',
          created_at: '2024-01-02T00:00:00Z'
        }
      ]
    }
  },

  // Bid responses
  bids: {
    created: {
      id: 'bid_new_123',
      status: 'PENDING',
      instance_type: 'gpu.a100.1x',
      region: 'us-west-2',
      created_at: '2024-01-15T10:30:00Z',
      auction_id: 'auction_a100_west',
      project: 'test-project'
    },
    running: {
      id: 'bid_running_456',
      status: 'RUNNING',
      instance_type: 'gpu.a100.1x',
      region: 'us-west-2',
      created_at: '2024-01-15T10:00:00Z',
      auction_id: 'auction_a100_west',
      project: 'test-project',
      instance: {
        id: 'instance_789',
        status: 'RUNNING',
        public_ip: '54.123.45.67',
        private_ip: '10.0.1.23',
        launched_at: '2024-01-15T10:05:00Z'
      }
    },
    provisioning: {
      id: 'bid_provisioning_789',
      status: 'PROVISIONING',
      instance_type: 'gpu.h100.2x',
      region: 'us-east-1',
      created_at: '2024-01-15T10:25:00Z',
      auction_id: 'auction_h100_east',
      project: 'test-project',
      instance: {
        id: 'instance_provisioning',
        status: 'PROVISIONING',
        launched_at: '2024-01-15T10:26:00Z'
      }
    },
    terminated: {
      id: 'bid_terminated_999',
      status: 'TERMINATED',
      instance_type: 'gpu.a100.1x',
      region: 'us-west-2',
      created_at: '2024-01-14T08:00:00Z',
      terminated_at: '2024-01-14T12:00:00Z',
      auction_id: 'auction_old',
      project: 'test-project'
    }
  },

  // List bids response
  bidsList: {
    mixed: {
      bids: [
        {
          id: 'bid_running_1',
          status: 'RUNNING',
          instance_type: 'gpu.a100.1x',
          region: 'us-west-2',
          created_at: '2024-01-15T08:00:00Z'
        },
        {
          id: 'bid_pending_2',
          status: 'PENDING',
          instance_type: 'gpu.h100.2x',
          region: 'us-east-1',
          created_at: '2024-01-15T10:30:00Z'
        },
        {
          id: 'bid_terminated_3',
          status: 'TERMINATED',
          instance_type: 'gpu.a100.1x',
          region: 'us-west-2',
          created_at: '2024-01-14T08:00:00Z'
        },
        {
          id: 'bid_failed_4',
          status: 'FAILED',
          instance_type: 'gpu.h100.8x',
          region: 'us-east-1',
          created_at: '2024-01-15T09:00:00Z',
          error: 'Insufficient capacity'
        }
      ]
    },
    empty: {
      bids: []
    }
  },

  // Error responses
  errors: {
    unauthorized: {
      status: 401,
      body: {
        error: 'unauthorized',
        message: 'Invalid API key'
      }
    },
    notFound: {
      status: 404,
      body: {
        error: 'not_found',
        message: 'Bid not found'
      }
    },
    badRequest: {
      status: 400,
      body: {
        error: 'bad_request',
        message: 'Invalid instance type'
      }
    },
    rateLimited: {
      status: 429,
      body: {
        error: 'rate_limited',
        message: 'Too many requests',
        retry_after: 60
      }
    },
    serverError: {
      status: 500,
      body: {
        error: 'internal_error',
        message: 'Internal server error'
      }
    }
  }
}

/**
 * Get a fixture by path (e.g., 'availability.withInstances').
 */
export function getFixture(path) {
  const parts = path.split('.')
  let current = fixtures
  
  for (const part of parts) {
    current = current[part]
    if (!current) {
      throw new Error(`Fixture not found: ${path}`)
    }
  }
  
  return JSON.parse(JSON.stringify(current))  // Deep clone
}