"""
Shared configuration for all cloud deployments.
Centralizes app-specific settings that are cloud-agnostic.
"""

from dataclasses import dataclass
from typing import Optional
import pulumi


@dataclass
class AppConfig:
    """Application configuration shared across all cloud providers."""

    # Naming
    project_name: str = "amar"
    environment: str = "production"  # production, staging, dev

    # Compute sizing
    app_cpu: int = 2          # vCPUs
    app_memory_gb: int = 4    # GB RAM

    # Database
    db_name: str = "amarback"
    db_user: str = "amaruser"
    db_version: str = "15"    # PostgreSQL version
    db_size_gb: int = 10      # Storage

    # Redis
    redis_version: str = "7"
    redis_memory_mb: int = 1024

    # Networking
    allowed_ssh_ips: list = None  # IPs allowed to SSH (None = your IP only)
    domain: Optional[str] = "amarmascotas.ar"

    def __post_init__(self):
        if self.allowed_ssh_ips is None:
            self.allowed_ssh_ips = []

    @property
    def resource_prefix(self) -> str:
        """Prefix for all resource names."""
        return f"{self.project_name}-{self.environment}"

    @property
    def tags(self) -> dict:
        """Common tags for all resources."""
        return {
            "Project": self.project_name,
            "Environment": self.environment,
            "ManagedBy": "Pulumi",
        }


def get_config() -> AppConfig:
    """Load configuration from Pulumi config or use defaults."""
    config = pulumi.Config()

    return AppConfig(
        project_name=config.get("project_name") or "amar",
        environment=config.get("environment") or "production",
        app_memory_gb=config.get_int("app_memory_gb") or 4,
        db_name=config.get("db_name") or "amarback",
        db_user=config.get("db_user") or "amaruser",
        domain=config.get("domain") or "amarmascotas.ar",
    )


# Cloud-init script for app server setup
APP_SERVER_INIT_SCRIPT = """#!/bin/bash
set -e

# Update system
apt-get update
apt-get upgrade -y

# Install dependencies
apt-get install -y \\
    python3-pip python3-venv \\
    postgresql-client \\
    gdal-bin libgdal-dev libgeos-dev libproj-dev \\
    nginx certbot python3-certbot-nginx \\
    supervisor \\
    git

# Create app user
useradd -m -s /bin/bash amarapp || true

# Create directories
mkdir -p /var/www/amarmascotas/media
mkdir -p /var/etc/static
mkdir -p /home/amarapp/app
chown -R amarapp:amarapp /var/www/amarmascotas
chown -R amarapp:amarapp /var/etc/static
chown -R amarapp:amarapp /home/amarapp

echo "Base setup complete. Deploy application code separately."
"""
