// Author: Dr Hamid MADANI <drmdh@msn.com>
//
// Server component wrapper — reads runtime config via @mostajs/config and
// passes the resolved defaults to the client studio page.
//
// No 'use client' directive ⇒ this file runs on the Next.js server. The
// process.env reads happen at request time, so editing .env + restarting
// pm2 picks up new defaults WITHOUT a Next.js rebuild.
//
// Profile cascade (from @mostajs/config) :
//   MOSTA_ENV=DEV   →  DEV_STUDIO_AUDIO_STORAGE  →  STUDIO_AUDIO_STORAGE  →  fallback
//
// Backward-compat : if neither STUDIO_* nor MOSTA_ENV is set, the client
// page falls back to the legacy NEXT_PUBLIC_* env vars (build-time inlined).

import { getEnv, getEnvBool, getEnvNumber } from '@mostajs/config'
import StudioPage from '@mostajs/media/pages/CaptureEditorPage'
import type { PageDefaults } from '@mostajs/media/pages/CaptureEditorPage'
import type {
  AudioRecordStorage, OutputFormat, ChunkStorage,
} from '@mostajs/media'

type RecordingMode = 'screen-record' | 'audio-only'

export default function Page() {
  const defaults: PageDefaults = {
    recordingMode:         getEnv('STUDIO_RECORDING_MODE', 'screen-record') as RecordingMode,
    outputFormat:          getEnv('STUDIO_OUTPUT_FORMAT', 'mp4') as OutputFormat,
    micEnabled:            getEnvBool('STUDIO_MIC_ENABLED', true),
    webcamEnabled:         getEnvBool('STUDIO_WEBCAM_ENABLED', true),
    systemAudioEnabled:    getEnvBool('STUDIO_SYSTEM_AUDIO_ENABLED', true),
    recordAudioSeparately: getEnvBool('STUDIO_RECORD_AUDIO_SEPARATELY', false),
    audioStorage:          getEnv('STUDIO_AUDIO_STORAGE', 'memory') as AudioRecordStorage,
    audioServerUrl:        getEnv('STUDIO_AUDIO_SERVER_URL', '/api/record/audio'),
    videoStorage:          getEnv('STUDIO_VIDEO_STORAGE', 'indexeddb') as ChunkStorage,
    videoServerUrl:        getEnv('STUDIO_VIDEO_SERVER_URL', '/api/record/video'),
    videoOnly:             getEnvBool('STUDIO_VIDEO_ONLY', false),
    logo: getEnv('STUDIO_LOGO_URL') ? {
      url:       getEnv('STUDIO_LOGO_URL'),
      position:  getEnv('STUDIO_LOGO_POSITION', 'top-right') as 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left',
      sizePct:   getEnvNumber('STUDIO_LOGO_SIZE_PCT',   10),
      opacity:   getEnvNumber('STUDIO_LOGO_OPACITY',    0.85),
      marginPct: getEnvNumber('STUDIO_LOGO_MARGIN_PCT', 2),
    } : undefined,
    // v1.12.0 — caméra par défaut (deviceId stable une fois choisie ; labelHint
    // robuste face aux rotations d'IDs après reset privacy).
    cameraDeviceId:  getEnv('STUDIO_CAMERA_DEVICE_ID')   || undefined,
    cameraLabelHint: getEnv('STUDIO_CAMERA_LABEL_HINT')  || undefined,
  }
  return <StudioPage defaults={defaults} />
}
