import { vi } from 'vitest'

/** Default mock query handler for integration tests (no live Postgres). */
export function createIntegrationMockQuery() {
  const metricsRows = Array.from({ length: 20 }, (_, i) => ({
    timestamp: new Date(Date.now() - (20 - i) * 60000),
    value: (50 + (i === 15 ? 150 : 0)).toString(),
    labels: {},
  }))

  return vi.fn().mockImplementation((sql: string) => {
    const q = sql.toLowerCase()

    if (q.includes('from metrics')) {
      return Promise.resolve({ rows: metricsRows })
    }
    if (q.includes('from tenants')) {
      return Promise.resolve({
        rows: [
          {
            id: 'tenant-1',
            quota_limits: {
              compute: { vcpu: 64, memory: 256, instances: 100 },
              storage: { total: 10000 },
              network: { bandwidth: 10000, egress: 5000 },
            },
            metadata: { region: 'US' },
          },
        ],
      })
    }
    if (q.includes('from pillars')) {
      return Promise.resolve({
        rows: [
          {
            id: 'pillar-1',
            code: 'OPERATIONAL_EXCELLENCE',
            name: 'Operational Excellence',
            description: 'Ops pillar',
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })
    }
    if (q.includes('from controls')) {
      return Promise.resolve({
        rows: [
          {
            id: 'control-1',
            pillar_id: 'pillar-1',
            code: 'C1',
            name: 'Control 1',
            description: 'Test control',
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })
    }
    if (q.includes('insert into resources')) {
      return Promise.resolve({
        rows: [
          {
            id: 'resource-new-1',
            name: 'test-vm',
            type: 'VM',
            status: 'PENDING',
            site_id: 'site-1',
            tenant_id: null,
            metadata: JSON.stringify({ cpu: 4, memory: '8Gi' }),
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })
    }
    if (q.includes('from resources') && q.includes('where id')) {
      return Promise.resolve({
        rows: [
          {
            id: 'resource-new-1',
            name: 'test-vm',
            type: 'VM',
            status: 'PENDING',
            site_id: 'site-1',
            tenant_id: null,
            metadata: JSON.stringify({ cpu: 4, memory: '8Gi' }),
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })
    }
    if (q.includes('from resources')) {
      return Promise.resolve({ rows: [] })
    }
    if (q.includes('from sites')) {
      return Promise.resolve({
        rows: [
          {
            id: 'site-1',
            name: 'Site 1',
            region: 'us-east-1',
            status: 'ACTIVE',
            metadata: '{}',
            created_at: new Date(),
            updated_at: new Date(),
          },
        ],
      })
    }
    if (q.includes('from findings')) {
      return Promise.resolve({ rows: [] })
    }
    if (q.includes('insert into') || q.includes('update ')) {
      return Promise.resolve({ rows: [] })
    }

    return Promise.resolve({ rows: [] })
  })
}

export function createMockDbPool(query = createIntegrationMockQuery()) {
  return { query }
}
