import { registerAs } from '@nestjs/config';
import * as Joi from 'joi';

export const appConfigValidationSchema = Joi.object({
  NODE_ENV: Joi.string().valid('development', 'production', 'test').default('development'),
  PORT: Joi.number().default(3001),
  API_VERSION: Joi.string().default('v1'),
  API_PREFIX: Joi.string().default('api'),

  DATABASE_HOST: Joi.string().required(),
  DATABASE_PORT: Joi.number().default(5432),
  DATABASE_NAME: Joi.string().required(),
  DATABASE_USER: Joi.string().required(),
  DATABASE_PASSWORD: Joi.string().required(),
  DATABASE_SSL: Joi.boolean().default(false),

  BESU_RPC_URL: Joi.string().uri().required(),
  BESU_CHAIN_ID: Joi.number().required(),
  CONTRACT_ADDRESS_LICENSE_NFT: Joi.string().allow('').default(''),
  CONTRACT_ADDRESS_APPROVAL_MANAGER: Joi.string().allow('').default(''),
  CONTRACT_ADDRESS_DEPARTMENT_REGISTRY: Joi.string().allow('').default(''),
  CONTRACT_ADDRESS_WORKFLOW_REGISTRY: Joi.string().allow('').default(''),
  PLATFORM_WALLET_PRIVATE_KEY: Joi.string().allow('').default(''),

  MINIO_ENDPOINT: Joi.string().required(),
  MINIO_PORT: Joi.number().default(9000),
  MINIO_ACCESS_KEY: Joi.string().required(),
  MINIO_SECRET_KEY: Joi.string().required(),
  MINIO_BUCKET_DOCUMENTS: Joi.string().default('goa-gel-documents'),
  MINIO_USE_SSL: Joi.boolean().default(false),

  REDIS_HOST: Joi.string().default('localhost'),
  REDIS_PORT: Joi.number().default(6379),
  REDIS_PASSWORD: Joi.string().allow('').default(''),

  JWT_SECRET: Joi.string().min(32).required(),
  JWT_EXPIRATION: Joi.string().default('1d'),
  API_KEY_SALT_ROUNDS: Joi.number().default(10),

  MAX_FILE_SIZE: Joi.number().default(10485760),
  ALLOWED_MIME_TYPES: Joi.string().default('application/pdf,image/jpeg,image/png'),

  RATE_LIMIT_GLOBAL: Joi.number().default(100),
  RATE_LIMIT_API_KEY: Joi.number().default(1000),

  LOG_LEVEL: Joi.string().valid('error', 'warn', 'info', 'debug').default('info'),

  CORS_ORIGIN: Joi.string().default('http://localhost:3000'),
  SWAGGER_ENABLED: Joi.boolean().default(true),
});

export default registerAs('app', () => ({
  nodeEnv: process.env.NODE_ENV || 'development',
  port: parseInt(process.env.PORT || '3001', 10),
  apiVersion: process.env.API_VERSION || 'v1',
  apiPrefix: process.env.API_PREFIX || 'api',
  corsOrigin: process.env.CORS_ORIGIN || 'http://localhost:3000',
  swaggerEnabled: process.env.SWAGGER_ENABLED === 'true',
}));
