# Database Validator - Database configuration validation
# Version: 1.0.0
#
# Validates database configurations against constraints

let Common = import "common-validator.ncl" in

{
  # Validate database type
  validate_database_type = fun db_type =>
    Common.validate_one_of ['sqlite, 'postgres, 'mysql, 'surrealdb, 'none] db_type,

  # Validate database name (alphanumeric + underscore)
  validate_database_name = fun name =>
    Common.validate_pattern "^[a-zA-Z0-9_]+$" name &&
    Common.validate_string_length 1 64 name,

  # Validate hostname
  validate_hostname = fun host =>
    Common.validate_string_length 1 256 host,

  # Validate password
  validate_password = fun password =>
    Common.validate_password password,

  # Validate connection pool size
  validate_pool_size = fun size =>
    Common.validate_number_range 1 1000 size,

  # Validate timeout
  validate_timeout = fun timeout =>
    Common.validate_number_range 1 300 timeout,

  # SQLite validation
  validate_sqlite = {
    database_path
      | String
      | {
        label = "valid path",
        predicate = Common.validate_string_length 1 256
      },

    journal_mode
      | [| 'WAL, 'DELETE, 'TRUNCATE, 'PERSIST, 'MEMORY |]
      | default = 'WAL,

    synchronous
      | [| 'OFF, 'NORMAL, 'FULL, 'EXTRA |]
      | default = 'NORMAL,

    cache_size | Number | default = -2000,

    foreign_keys | Bool | default = true,
  },

  # PostgreSQL validation
  validate_postgres = {
    host
      | String
      | {
        label = "valid hostname",
        predicate = validate_hostname
      }
      | default = "localhost",

    port
      | Number
      | {
        label = "valid port",
        predicate = Common.validate_host_port
      }
      | default = 5432,

    database
      | String
      | {
        label = "valid database name",
        predicate = validate_database_name
      },

    username
      | String
      | {
        label = "valid username",
        predicate = Common.validate_string_length 1 64
      },

    password
      | String
      | {
        label = "valid password",
        predicate = validate_password
      },

    pool_size
      | Number
      | {
        label = "valid pool size",
        predicate = validate_pool_size
      }
      | default = 10,

    connection_timeout
      | Number
      | {
        label = "valid timeout",
        predicate = validate_timeout
      }
      | default = 30,

    idle_timeout
      | Number
      | {
        label = "valid idle timeout",
        predicate = Common.validate_number_range 60 3600
      }
      | default = 600,

    max_lifetime
      | Number
      | {
        label = "valid max lifetime",
        predicate = Common.validate_number_range 300 86400
      }
      | default = 3600,

    ssl_mode
      | [| 'disable, 'allow, 'prefer, 'require, 'verify-ca, 'verify-full |]
      | default = 'prefer,

    application_name
      | String
      | optional,
  },

  # MySQL validation
  validate_mysql = {
    host
      | String
      | {
        label = "valid hostname",
        predicate = validate_hostname
      }
      | default = "localhost",

    port
      | Number
      | {
        label = "valid port",
        predicate = Common.validate_host_port
      }
      | default = 3306,

    database
      | String
      | {
        label = "valid database name",
        predicate = validate_database_name
      },

    username
      | String
      | {
        label = "valid username",
        predicate = Common.validate_string_length 1 32
      },

    password
      | String
      | {
        label = "valid password",
        predicate = validate_password
      },

    pool_size
      | Number
      | {
        label = "valid pool size",
        predicate = validate_pool_size
      }
      | default = 10,

    connection_timeout
      | Number
      | {
        label = "valid timeout",
        predicate = validate_timeout
      }
      | default = 30,

    charset | String | default = "utf8mb4",

    collation | String | default = "utf8mb4_unicode_ci",
  },

  # SurrealDB validation
  validate_surrealdb = {
    host
      | String
      | {
        label = "valid hostname",
        predicate = validate_hostname
      }
      | default = "localhost",

    port
      | Number
      | {
        label = "valid port",
        predicate = Common.validate_host_port
      }
      | default = 8000,

    namespace
      | String
      | {
        label = "valid namespace",
        predicate = Common.validate_string_length 1 64
      },

    database
      | String
      | {
        label = "valid database name",
        predicate = Common.validate_string_length 1 64
      },

    username
      | String
      | {
        label = "valid username",
        predicate = Common.validate_string_length 1 64
      },

    password
      | String
      | {
        label = "valid password",
        predicate = validate_password
      },

    protocol
      | [| 'http, 'https, 'ws, 'wss |]
      | default = 'http,
  },

  # Complete database configuration validation
  validate_database_config = {
    type
      | {
        label = "valid database type",
        predicate = validate_database_type
      },

    sqlite
      | validate_sqlite
      | optional,

    postgres
      | validate_postgres
      | optional,

    mysql
      | validate_mysql
      | optional,

    surrealdb
      | validate_surrealdb
      | optional,

    migrations_path
      | String
      | default = "./migrations",

    auto_migrate
      | Bool
      | default = false,
  },
}
