# CloudLayer

A modular cloud application platform with optional Payment Gateway integration.

## Features

- 🏢 **DA Product App** - Main application with device management, MQTT, and business logic
- 💳 **Payment Gateway App** (Optional) - Standalone payment processing module
- 🔌 **Modular Architecture** - Enable/disable features at compile-time and runtime
- 🚀 **Phoenix LiveView** - Modern, real-time web interfaces
- 📊 **Comprehensive Testing** - Full test coverage for all modules

## Quick Start

### Prerequisites

- Elixir ~> 1.14
- Phoenix ~> 1.7
- MySQL database
- Node.js (for asset compilation)

### Installation

```bash
# Clone the repository
git clone https://github.com/momentpay/CloudLayer.git
cd CloudLayer

# Install dependencies
mix deps.get

# Setup database
mix ecto.setup

# Install and build assets
mix assets.setup
mix assets.build

# Start the application
mix phx.server
```

Visit http://localhost:4000

## Payment Gateway Module

The Payment Gateway is an **optional, independent OTP application** that can be enabled/disabled at compile-time and runtime.

### Enabling Payment Gateway

#### Compile-time (Include in build)

```bash
# Enable at compile time
export INCLUDE_PAYMENT_GATEWAY=true
mix deps.get
mix compile
```

#### Runtime (Enable when app starts)

```bash
# Enable at runtime
export ENABLE_PAYMENT_GATEWAY=true
mix phx.server
```

Or configure in `config/config.exs`:

```elixir
config :da_product_app, :enable_payment_gateway, true
```

### Using Payment Gateway

When enabled, the Payment Gateway exposes:

- **Web Interface**: http://localhost:4001/pgpayments
- **API Endpoints**: 
  - `POST /api/pgpayments/initiate` - Create payment
  - `GET /api/pgpayments/status/:payment_id` - Check status

#### Programmatic API

```elixir
# Check if Payment Gateway is available
if Code.ensure_loaded?(PaymentGatewayApp) do
  # Start a payment
  {:ok, payment} = PaymentGatewayApp.start_payment(100.00, "USD", "user_123")
  
  # Check status
  {:ok, status} = PaymentGatewayApp.get_payment_status(payment.id)
end
```

### Payment Gateway Documentation

For detailed Payment Gateway documentation, see [apps/payment_gateway_app/README.md](apps/payment_gateway_app/README.md)

## Project Structure

```
CloudLayer/
├── apps/
│   └── payment_gateway_app/     # Optional Payment Gateway module
├── assets/                      # Main app frontend assets
├── config/                      # Application configuration
├── lib/
│   ├── da_product_app/         # Core business logic
│   └── da_product_app_web/     # Web interface
├── priv/                        # Database migrations, static files
├── test/                        # Test suite
└── mix.exs                      # Project definition
```

## Configuration

Main configuration files:

- `config/config.exs` - Base configuration
- `config/dev.exs` - Development settings
- `config/prod.exs` - Production settings
- `config/test.exs` - Test environment
- `config/runtime.exs` - Runtime configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `INCLUDE_PAYMENT_GATEWAY` | Enable PG at compile-time | `false` |
| `ENABLE_PAYMENT_GATEWAY` | Enable PG at runtime | `false` |
| `PG_API_KEY` | Payment provider API key | - |
| `PG_API_SECRET` | Payment provider secret | - |
| `PG_API_ENDPOINT` | Payment provider URL | - |

## Development

### Running Tests

```bash
# Run all tests
mix test

# Run specific test file
mix test test/da_product_app/users_test.exs

# Run with coverage
mix test --cover

# Run Payment Gateway tests (if enabled)
cd apps/payment_gateway_app
mix test
```

### Code Quality

```bash
# Format code
mix format

# Run static analysis
mix credo

# Run security checks
mix sobelow
```

### Database Operations

```bash
# Create database
mix ecto.create

# Run migrations
mix ecto.migrate

# Rollback migration
mix ecto.rollback

# Reset database
mix ecto.reset

# Generate migration
mix ecto.gen.migration create_users
```

## Production Deployment

### Building Release

```bash
# With Payment Gateway
INCLUDE_PAYMENT_GATEWAY=true MIX_ENV=prod mix release

# Without Payment Gateway
MIX_ENV=prod mix release
```

### Running Release

```bash
# Enable Payment Gateway at runtime
ENABLE_PAYMENT_GATEWAY=true _build/prod/rel/da_product_app/bin/da_product_app start

# Without Payment Gateway
_build/prod/rel/da_product_app/bin/da_product_app start
```

## Architecture

### Main Application (DA Product App)

- **Device Management** - Register and manage devices
- **MQTT Integration** - Real-time messaging
- **User Management** - Authentication and authorization
- **Business Logic** - Core application features

### Payment Gateway (Optional Module)

- **Payment Initiation** - Create payment transactions
- **Status Tracking** - Monitor payment states
- **Provider Abstraction** - Support multiple payment providers
- **Web Interface** - Self-contained UI for payments

### Modular Design

The Payment Gateway demonstrates CloudLayer's modular architecture:

1. **Loose Coupling** - PG operates independently
2. **Optional Compilation** - Include/exclude at build time
3. **Runtime Toggle** - Enable/disable without recompilation
4. **Clean API** - Simple interface for integration

## Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## Testing Guidelines

- Write tests for all new features
- Maintain test coverage above 80%
- Follow existing test patterns
- Use descriptive test names

## License

[Your License Here]

## Support

For issues, questions, or contributions:
- Open an issue on GitHub
- Contact the development team
- Check the documentation in each module's README