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

export const up: Migration['up'] = async (db) => {
  // Blockchain networks table
  await db.query(`
    CREATE TABLE IF NOT EXISTS blockchain_networks (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      name VARCHAR(255) NOT NULL,
      framework VARCHAR(50) NOT NULL CHECK (framework IN (
        'HYPERLEDGER_FABRIC',
        'HYPERLEDGER_BESU',
        'HYPERLEDGER_INDY',
        'HYPERLEDGER_FIREFLY',
        'CACTI'
      )),
      deployment_id UUID REFERENCES deployments(id) ON DELETE SET NULL,
      network_id VARCHAR(255),
      status VARCHAR(50) NOT NULL DEFAULT 'PENDING' CHECK (status IN (
        'PENDING',
        'PROVISIONING',
        'RUNNING',
        'STOPPED',
        'FAILED'
      )),
      configuration JSONB DEFAULT '{}'::jsonb,
      metadata JSONB DEFAULT '{}'::jsonb,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  `)

  // Blockchain nodes table
  await db.query(`
    CREATE TABLE IF NOT EXISTS blockchain_nodes (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      network_id UUID NOT NULL REFERENCES blockchain_networks(id) ON DELETE CASCADE,
      name VARCHAR(255) NOT NULL,
      role VARCHAR(50) NOT NULL CHECK (role IN (
        'PEER',
        'ORDERER',
        'VALIDATOR',
        'ENDORSER',
        'CA',
        'IDENTITY_NODE',
        'FIREFLY_NODE',
        'CACTI_NODE'
      )),
      node_type VARCHAR(50) CHECK (node_type IN ('VM', 'CONTAINER', 'KUBERNETES')),
      endpoint_url VARCHAR(500),
      rpc_endpoint VARCHAR(500),
      grpc_endpoint VARCHAR(500),
      websocket_endpoint VARCHAR(500),
      status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
      configuration JSONB DEFAULT '{}'::jsonb,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  `)

  // Blockchain organizations table (for multi-org networks)
  await db.query(`
    CREATE TABLE IF NOT EXISTS blockchain_organizations (
      id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
      network_id UUID NOT NULL REFERENCES blockchain_networks(id) ON DELETE CASCADE,
      name VARCHAR(255) NOT NULL,
      msp_id VARCHAR(255),
      ca_certificate TEXT,
      admin_certificate TEXT,
      configuration JSONB DEFAULT '{}'::jsonb,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
      updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  `)

  // Indexes
  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_blockchain_networks_framework ON blockchain_networks(framework)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_blockchain_networks_status ON blockchain_networks(status)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_blockchain_nodes_network ON blockchain_nodes(network_id)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_blockchain_nodes_role ON blockchain_nodes(role)
  `)

  await db.query(`
    CREATE INDEX IF NOT EXISTS idx_blockchain_organizations_network ON blockchain_organizations(network_id)
  `)

  // Update triggers
  await db.query(`
    CREATE TRIGGER update_blockchain_networks_updated_at
    BEFORE UPDATE ON blockchain_networks
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column()
  `)

  await db.query(`
    CREATE TRIGGER update_blockchain_nodes_updated_at
    BEFORE UPDATE ON blockchain_nodes
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column()
  `)

  await db.query(`
    CREATE TRIGGER update_blockchain_organizations_updated_at
    BEFORE UPDATE ON blockchain_organizations
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column()
  `)
}

export const down: Migration['down'] = async (db) => {
  await db.query(`DROP TABLE IF EXISTS blockchain_organizations`)
  await db.query(`DROP TABLE IF EXISTS blockchain_nodes`)
  await db.query(`DROP TABLE IF EXISTS blockchain_networks`)
}

