# EU ETS Database Microservice

A standalone Axum-based microservice for managing EU ETS database operations.

## Features

- PostgreSQL database with SQLx
- RESTful API with Axum
- Type-safe request/response structs
- Docker Compose setup with pgAdmin
- Structured logging with tracing
- CORS enabled for cross-origin requests

## Prerequisites

- Rust (latest stable)
- Docker and Docker Compose
- PostgreSQL client (optional, for manual queries)

## Quick Start

1. **Start the database:**
```bash
   docker-compose up -d
```

2. **Run the service:**
```bash
   cargo run
```

3. **Test the API:**
```bash
   ./test_api.sh
```

## API Endpoints

### Local Admins
- `POST /api/registry/local-admins` - Create local admin
- `GET /api/registry/local-admins` - List all local admins
- `GET /api/registry/local-admins/:pubkey` - Get specific local admin
- `PUT /api/registry/local-admins/status` - Update local admin status

### Companies
- `POST /api/registry/companies` - Create company
- `GET /api/registry/companies` - List all companies
- `GET /api/registry/companies/:pubkey` - Get specific company
- `PUT /api/registry/companies/status` - Update company status
- `GET /api/registry/companies/by-local-admin/:pubkey` - List companies by local admin

### EUA Minting
- `POST /api/registry/eua-minting` - Log EUA minting
- `GET /api/registry/eua-minting/:company_pubkey` - Get EUA minting logs

### GHG Minting
- `POST /api/registry/ghg-minting` - Log GHG minting
- `GET /api/registry/ghg-minting/:company_pubkey` - Get GHG minting logs

### EUA Transfers
- `POST /api/registry/eua-transfers` - Log EUA transfer
- `GET /api/registry/eua-transfers/:company_pubkey` - Get EUA transfer logs

## Database Access

**pgAdmin:**
- URL: http://localhost:5050
- Email: admin@admin.com
- Password: admin123

**PostgreSQL:**
- Host: localhost
- Port: 5432
- Database: eu_ets_db
- User: admin
- Password: admin123

## Project Structure
```
database/
├── src/
│   ├── main.rs              # Application entry point
│   ├── config.rs            # Configuration management
│   ├── db.rs                # Database connection and migrations
│   ├── registry/
│   │   ├── mod.rs           # Registry routes
│   │   ├── types/mod.rs     # Request/Response types
│   │   ├── services/mod.rs  # Database operations
│   │   └── handlers/mod.rs  # HTTP handlers
│   └── secondary_market/    # Future expansion
├── migrations/
│   └── 001_create_tables.sql
├── docker-compose.yml
├── .env
└── Cargo.toml
```

## Environment Variables
```
DATABASE_URL=postgresql://admin:admin123@localhost:5432/eu_ets_db
SERVER_HOST=0.0.0.0
SERVER_PORT=3000
RUST_LOG=debug
```

## Development

**Run with auto-reload:**
```bash
cargo install cargo-watch
cargo watch -x run
```

**Run tests:**
```bash
cargo test
```

**Check code:**
```bash
cargo clippy
cargo fmt
```

## Integration with Main Project

See `example_event_handler.rs` for examples of how to integrate this service with your main Solana project's event handlers using `reqwest`.

Add to your main project's `Cargo.toml`:
```toml
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
serde_json = "1.0"
```

## Notes

- All pubkeys are stored as TEXT strings
- Timestamps are automatically managed (created_at, updated_at)
- Companies_managed counter is automatically incremented when creating companies
- Foreign key constraints ensure referential integrity
