import { Context } from '../types/context'

export interface StorageAccount {
  id: string
  name: string
  provider: string
  endpoint?: string
  metadata: Record<string, any>
  createdAt: Date
  updatedAt: Date
}

export interface StorageContainer {
  id: string
  accountId: string
  name: string
  type: string
  metadata: Record<string, any>
  createdAt: Date
}

export interface CreateStorageAccountInput {
  name: string
  provider: string
  endpoint?: string
  metadata?: Record<string, any>
}

export interface CreateStorageContainerInput {
  accountId: string
  name: string
  type: string
  metadata?: Record<string, any>
}

export async function getStorageAccounts(context: Context): Promise<StorageAccount[]> {
  const db = context.db
  const result = await db.query('SELECT * FROM storage_accounts ORDER BY created_at DESC')
  return result.rows.map(mapStorageAccount)
}

export async function getStorageAccount(context: Context, id: string): Promise<StorageAccount> {
  const db = context.db
  const result = await db.query('SELECT * FROM storage_accounts WHERE id = $1', [id])

  if (result.rows.length === 0) {
    throw new Error('Storage account not found')
  }

  return mapStorageAccount(result.rows[0])
}

export async function createStorageAccount(
  context: Context,
  input: CreateStorageAccountInput
): Promise<StorageAccount> {
  const db = context.db
  const result = await db.query(
    `INSERT INTO storage_accounts (name, provider, endpoint, metadata)
     VALUES ($1, $2, $3, $4)
     RETURNING *`,
    [
      input.name,
      input.provider,
      input.endpoint || null,
      JSON.stringify(input.metadata || {}),
    ]
  )

  return mapStorageAccount(result.rows[0])
}

export async function getStorageContainers(
  context: Context,
  accountId: string
): Promise<StorageContainer[]> {
  const db = context.db
  const result = await db.query(
    'SELECT * FROM storage_containers WHERE account_id = $1 ORDER BY created_at DESC',
    [accountId]
  )
  return result.rows.map(mapStorageContainer)
}

export async function createStorageContainer(
  context: Context,
  input: CreateStorageContainerInput
): Promise<StorageContainer> {
  const db = context.db
  const result = await db.query(
    `INSERT INTO storage_containers (account_id, name, type, metadata)
     VALUES ($1, $2, $3, $4)
     RETURNING *`,
    [
      input.accountId,
      input.name,
      input.type,
      JSON.stringify(input.metadata || {}),
    ]
  )

  return mapStorageContainer(result.rows[0])
}

function mapStorageAccount(row: any): StorageAccount {
  return {
    id: row.id,
    name: row.name,
    provider: row.provider,
    endpoint: row.endpoint,
    metadata: row.metadata || {},
    createdAt: row.created_at,
    updatedAt: row.updated_at,
  }
}

function mapStorageContainer(row: any): StorageContainer {
  return {
    id: row.id,
    accountId: row.account_id,
    name: row.name,
    type: row.type,
    metadata: row.metadata || {},
    createdAt: row.created_at,
  }
}

