output "resource_group_name" {
  description = "Name of the resource group"
  value       = data.azurerm_resource_group.main.name
}

output "aks_cluster_name" {
  description = "Name of the AKS cluster"
  value       = data.azurerm_kubernetes_cluster.main.name
}

output "postgresql_service_name" {
  description = "Name of the PostgreSQL service (in-cluster)"
  value       = kubernetes_service.postgresql.metadata[0].name
}

output "postgresql_service_fqdn" {
  description = "Internal FQDN of the PostgreSQL service (in-cluster)"
  value       = "postgresql.${kubernetes_namespace.app.metadata[0].name}.svc.cluster.local"
}

output "postgresql_database_name" {
  description = "Name of the PostgreSQL database"
  value       = local.postgresql_db_name
}

output "postgresql_admin_username" {
  description = "Administrator username for PostgreSQL"
  value       = var.postgresql_admin_username
  sensitive   = true
}

output "postgresql_connection_string" {
  description = "PostgreSQL connection string (from within cluster)"
  value       = "postgresql://${var.postgresql_admin_username}:${nonsensitive(local.postgresql_admin_pass)}@postgresql.${kubernetes_namespace.app.metadata[0].name}.svc.cluster.local:5432/${local.postgresql_db_name}"
  sensitive   = true
}

output "app_namespace" {
  description = "Kubernetes namespace for the application"
  value       = kubernetes_namespace.app.metadata[0].name
}

output "app_service_name" {
  description = "Kubernetes service name for the application"
  value       = kubernetes_service.app.metadata[0].name
}

output "ingress_enabled" {
  description = "Whether ingress is enabled"
  value       = var.ingress_enabled
}

output "ingress_ip" {
  description = "IP address of the ingress controller load balancer"
  value       = var.ingress_enabled ? try(data.kubernetes_service.ingress_nginx[0].status[0].load_balancer[0].ingress[0].ip, "pending") : "not enabled"
}

output "app_url" {
  description = "URL of the Laravel application"
  value       = var.ingress_enabled ? (var.ingress_host != "" ? "https://${var.ingress_host}" : "http://${try(data.kubernetes_service.ingress_nginx[0].status[0].load_balancer[0].ingress[0].ip, "pending")}") : "ingress not enabled"
}

output "ssl_enabled" {
  description = "Whether SSL/TLS is enabled"
  value       = var.ssl_enabled
}

output "db_restore_info" {
  description = "Database restore information"
  value       = "Database restore disabled in Terraform. Use manual restore via kubectl (see db-restore.tf for instructions)"
}

output "deployment_instructions" {
  description = "Next steps after deployment"
  value       = <<-EOT
    ========================================
    Deployment Complete!
    ========================================

    1. Get kubectl credentials:
       az aks get-credentials --resource-group ${data.azurerm_resource_group.main.name} --name ${data.azurerm_kubernetes_cluster.main.name}

    2. Check application status:
       kubectl get pods -n ${kubernetes_namespace.app.metadata[0].name}
       kubectl get svc -n ${kubernetes_namespace.app.metadata[0].name}
       kubectl get ingress -n ${kubernetes_namespace.app.metadata[0].name}

    3. Check database restore job (if enabled):
       kubectl get jobs -n ${kubernetes_namespace.app.metadata[0].name}
       kubectl logs -n ${kubernetes_namespace.app.metadata[0].name} -l job-type=database-restore

    4. Access the application:
       ${var.ingress_enabled ? (var.ingress_host != "" ? "https://${var.ingress_host}" : "http://${try(data.kubernetes_service.ingress_nginx[0].status[0].load_balancer[0].ingress[0].ip, "pending")}") : "ingress not enabled - use port-forward"}

    5. Port-forward (if ingress not ready):
       kubectl port-forward -n ${kubernetes_namespace.app.metadata[0].name} svc/${kubernetes_service.app.metadata[0].name} 8080:80

    6. View logs:
       kubectl logs -n ${kubernetes_namespace.app.metadata[0].name} -l app=${var.app_name} -f

    7. Database connection details (in-cluster):
       Host: postgresql.${kubernetes_namespace.app.metadata[0].name}.svc.cluster.local
       Database: ${local.postgresql_db_name}
       Username: ${var.postgresql_admin_username}

    8. Restore database manually:
       kubectl cp ../backups/backup_backend_20251203_101741.dump ${kubernetes_namespace.app.metadata[0].name}/postgresql-0:/tmp/backup.dump
       kubectl exec -it -n ${kubernetes_namespace.app.metadata[0].name} postgresql-0 -- pg_restore -U ${var.postgresql_admin_username} -d ${local.postgresql_db_name} --clean --if-exists /tmp/backup.dump

    ========================================
    EOT
}
