#!/usr/bin/env bash
#
# Panel (Laravel app) deployment
#
# Epic 17 (Installer Script) — Phase 1 Epic 1-16 hazır olunca tamamlanır

panel_create_system_user() {
  log_info "→ onx_panel sistem kullanıcısı"
  if ! id onx_panel &>/dev/null; then
    run_or_die "onx_panel useradd" useradd -r -d "$ONOXSOFT_PANEL_DIR" \
      -s /sbin/nologin -c "ONOXSOFT panel daemon" onx_panel
  fi
}

panel_clone_or_extract() {
  log_info "→ Panel kodu deploy"
  # TODO Epic 17:
  # - Production: get.onox.com.tr'dan tar.gz indir + extract
  # - Veya: git clone (lisans server'dan signed tarball verir)
  # - composer install --no-dev --optimize-autoloader
  # - npm ci + npm run build
  # - chown -R onx_panel:onx_panel /usr/local/onoxsoft/panel
  log_warn "  TODO: Epic 17 sonrası doldurulacak"
}

panel_composer_install() {
  log_info "→ composer install --no-dev"
  # TODO: cd ${ONOXSOFT_PANEL_DIR} && sudo -u onx_panel composer install ...
  log_warn "  TODO: Epic 17"
}

panel_npm_build() {
  log_info "→ npm run build"
  # TODO: vite build
  log_warn "  TODO: Epic 17"
}

panel_setup_env() {
  log_info "→ .env ve APP_KEY"
  # TODO Epic 17:
  # - .env.example'ı .env'ye kopyala
  # - APP_KEY generate
  # - DB_DATABASE=onoxsoft_panel, DB_USERNAME, DB_PASSWORD random
  # - REDIS_PASSWORD random
  # - APP_URL=https://${ONOXSOFT_HOSTNAME}
  # - chmod 0600 .env (apache:apache)
  log_warn "  TODO: Epic 17"
}

panel_run_migrations() {
  log_info "→ php artisan migrate --force"
  # TODO: cd ${ONOXSOFT_PANEL_DIR} && sudo -u onx_panel php artisan migrate --force
  log_warn "  TODO: Epic 17"
}

panel_seed_initial_admin() {
  log_info "→ İlk admin kullanıcı"
  # TODO Epic 17:
  # - php artisan tinker veya custom command:
  #     php artisan onoxsoft:install-admin --email=${ONOXSOFT_ADMIN_EMAIL}
  # - Random parola generate, print_summary'de göster
  log_warn "  TODO: Epic 17"
}

panel_setup_vhost() {
  log_info "→ Panel Apache vhost"
  # TODO Epic 17:
  # - /etc/httpd/conf.d/sites/onoxsoft-panel.conf
  # - <VirtualHost *:443> with SSL + Inertia/Laravel
  # - DocumentRoot ${ONOXSOFT_PANEL_DIR}/public
  # - FPM proxy → /run/php-fpm/onoxsoft-panel.sock
  log_warn "  TODO: Epic 17"
}

panel_issue_ssl() {
  log_info "→ Let's Encrypt SSL (panel)"
  # TODO Epic 17 (Epic 9 hazır olduktan sonra):
  # - acme.sh ile HTTP-01 challenge
  # - ${ONOXSOFT_HOSTNAME} için sertifika al
  # - Apache reload hook
  log_warn "  TODO: Epic 9 + Epic 17"
}

panel_setup_queue_worker() {
  log_info "→ Horizon systemd unit"
  # TODO Epic 17:
  # - /etc/systemd/system/onoxsoft-horizon.service
  # - User=onx_panel
  # - ExecStart=/usr/local/bin/php artisan horizon
  # - Restart=always
  # - systemctl enable --now onoxsoft-horizon
  log_warn "  TODO: Epic 17"
}
