import { Migration } from '../migrate.js'

export const up: Migration['up'] = async (db) => {
  // Storage accounts table
  await db.query(`
    CREATE TABLE IF NOT EXISTS storage_accounts (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      name VARCHAR(255) NOT NULL UNIQUE,
      provider VARCHAR(50) NOT NULL CHECK (provider IN ('MINIO', 'CEPH', 'S3')),
      endpoint VARCHAR(500),
      access_key VARCHAR(255),
      metadata JSONB DEFAULT '{}'::jsonb,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  `)

  // Storage containers table
  await db.query(`
    CREATE TABLE IF NOT EXISTS storage_containers (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      account_id UUID NOT NULL REFERENCES storage_accounts(id) ON DELETE CASCADE,
      name VARCHAR(255) NOT NULL,
      type VARCHAR(50) NOT NULL CHECK (type IN ('BUCKET', 'CONTAINER', 'VOLUME')),
      metadata JSONB DEFAULT '{}'::jsonb,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      UNIQUE(account_id, name)
    )
  `)

  // Indexes for storage
  await db.query(`CREATE INDEX IF NOT EXISTS idx_storage_containers_account ON storage_containers(account_id)`)
}

export const down: Migration['down'] = async (db) => {
  await db.query(`DROP INDEX IF EXISTS idx_storage_containers_account`)
  await db.query(`DROP TABLE IF EXISTS storage_containers`)
  await db.query(`DROP TABLE IF EXISTS storage_accounts`)
}

