class postgresql::pgadmin {
  contain postgresql::pgadmin::install
  contain postgresql::pgadmin::config
}


class postgresql::pgadmin::install {
  apt::source { 'pgadmin':
    comment  => 'PGAdmin Repo',
    location => 'https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/bookworm',
    release  => 'pgadmin4',
    repos    => 'main',
    key      => {
      'name'   => 'pgadmin.pub',
      'source' => 'https://www.pgadmin.org/static/packages_pgadmin_org.pub',
    },
    include  => {
      'deb' => true,
    },
  }

  package { 'pgadmin4-web':
    ensure => installed
  }
}

class postgresql::pgadmin::config {
  exec { 'pgadmin-setup':
    command => '/usr/pgadmin4/bin/setup-web.sh --yes',
    environment => [
      "PGADMIN_SETUP_EMAIL=$pg_setup_email",
      "PGADMIN_SETUP_PASSWORD=$pg_setup_password"
    ],
    path => ['/usr/bin'],
    unless => ['test -f /var/lib/pgadmin', 'test -f /var/log/pgadmin']
  }

  file { '/usr/pgadmin4/web/config_local.py':
    ensure => file,
    content => template('postgresql/config_local.py.erb')
  }
}
