package config

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type Config struct {
	Audio   AudioConfig   `json:"audio"`
	RDS     RDSConfig     `json:"rds"`
	FM      FMConfig      `json:"fm"`
	Backend BackendConfig `json:"backend"`
	Control ControlConfig `json:"control"`
	Runtime RuntimeConfig `json:"runtime"`
	Ingest  IngestConfig  `json:"ingest"`
}

type AudioConfig struct {
	InputPath     string  `json:"inputPath"`
	Gain          float64 `json:"gain"`
	ToneLeftHz    float64 `json:"toneLeftHz"`
	ToneRightHz   float64 `json:"toneRightHz"`
	ToneAmplitude float64 `json:"toneAmplitude"`
}

type RDSConfig struct {
	Enabled   bool   `json:"enabled"`
	PI        string `json:"pi"`
	PS        string `json:"ps"`
	RadioText string `json:"radioText"`
	PTY       int    `json:"pty"`
}

type FMConfig struct {
	FrequencyMHz        float64 `json:"frequencyMHz"`
	StereoEnabled       bool    `json:"stereoEnabled"`
	PilotLevel          float64 `json:"pilotLevel"`       // fraction of ±75kHz deviation (0.09 = 9%, ITU standard)
	RDSInjection        float64 `json:"rdsInjection"`     // fraction of ±75kHz deviation (0.04 = 4%, typical)
	PreEmphasisTauUS    float64 `json:"preEmphasisTauUS"` // time constant in µs: 50 (EU) or 75 (US), 0=off
	OutputDrive         float64 `json:"outputDrive"`
	CompositeRateHz     int     `json:"compositeRateHz"` // internal DSP/MPX sample rate
	MaxDeviationHz      float64 `json:"maxDeviationHz"`
	LimiterEnabled      bool    `json:"limiterEnabled"`
	LimiterCeiling      float64 `json:"limiterCeiling"`
	FMModulationEnabled bool    `json:"fmModulationEnabled"`
	MpxGain             float64 `json:"mpxGain"`           // hardware calibration: scales entire composite output (default 1.0)
	BS412Enabled        bool    `json:"bs412Enabled"`      // ITU-R BS.412 MPX power limiter (EU requirement)
	BS412ThresholdDBr   float64 `json:"bs412ThresholdDBr"` // power limit in dBr (0 = standard, +3 = relaxed)
}

type BackendConfig struct {
	Kind               string            `json:"kind"`
	Driver             string            `json:"driver,omitempty"`
	Device             string            `json:"device"`
	URI                string            `json:"uri,omitempty"`
	DeviceArgs         map[string]string `json:"deviceArgs,omitempty"`
	OutputPath         string            `json:"outputPath"`
	DeviceSampleRateHz float64           `json:"deviceSampleRateHz"` // actual SDR device rate; 0 = same as compositeRateHz
}

type ControlConfig struct {
	ListenAddress string `json:"listenAddress"`
}

type RuntimeConfig struct {
	FrameQueueCapacity int `json:"frameQueueCapacity"`
}

type IngestConfig struct {
	Kind           string                `json:"kind"`
	PrebufferMs    int                   `json:"prebufferMs"`
	StallTimeoutMs int                   `json:"stallTimeoutMs"`
	Reconnect      IngestReconnectConfig `json:"reconnect"`
	Stdin          IngestPCMConfig       `json:"stdin"`
	HTTPRaw        IngestPCMConfig       `json:"httpRaw"`
	Icecast        IngestIcecastConfig   `json:"icecast"`
	SRT            IngestSRTConfig       `json:"srt"`
	AES67          IngestAES67Config     `json:"aes67"`
}

type IngestReconnectConfig struct {
	Enabled          bool `json:"enabled"`
	InitialBackoffMs int  `json:"initialBackoffMs"`
	MaxBackoffMs     int  `json:"maxBackoffMs"`
}

type IngestPCMConfig struct {
	SampleRateHz int    `json:"sampleRateHz"`
	Channels     int    `json:"channels"`
	Format       string `json:"format"`
}

type IngestIcecastConfig struct {
	URL       string                       `json:"url"`
	Decoder   string                       `json:"decoder"`
	RadioText IngestIcecastRadioTextConfig `json:"radioText"`
}

type IngestIcecastRadioTextConfig struct {
	Enabled      bool   `json:"enabled"`
	Prefix       string `json:"prefix"`
	MaxLen       int    `json:"maxLen"`
	OnlyOnChange bool   `json:"onlyOnChange"`
}

type IngestSRTConfig struct {
	URL          string `json:"url"`
	Mode         string `json:"mode"`
	SampleRateHz int    `json:"sampleRateHz"`
	Channels     int    `json:"channels"`
}

type IngestAES67Config struct {
	SDPPath            string                     `json:"sdpPath"`
	SDP                string                     `json:"sdp"`
	Discovery          IngestAES67DiscoveryConfig `json:"discovery"`
	MulticastGroup     string                     `json:"multicastGroup"`
	Port               int                        `json:"port"`
	InterfaceName      string                     `json:"interfaceName"`
	PayloadType        int                        `json:"payloadType"`
	SampleRateHz       int                        `json:"sampleRateHz"`
	Channels           int                        `json:"channels"`
	Encoding           string                     `json:"encoding"`
	PacketTimeMs       int                        `json:"packetTimeMs"`
	JitterDepthPackets int                        `json:"jitterDepthPackets"`
	ReadBufferBytes    int                        `json:"readBufferBytes"`
}

type IngestAES67DiscoveryConfig struct {
	Enabled       bool   `json:"enabled"`
	StreamName    string `json:"streamName"`
	TimeoutMs     int    `json:"timeoutMs"`
	InterfaceName string `json:"interfaceName"`
	SAPGroup      string `json:"sapGroup"`
	SAPPort       int    `json:"sapPort"`
}

func Default() Config {
	return Config{
		Audio: AudioConfig{Gain: 1.0, ToneLeftHz: 1000, ToneRightHz: 1600, ToneAmplitude: 0.4},
		RDS:   RDSConfig{Enabled: true, PI: "1234", PS: "FMRTX", RadioText: "fm-rds-tx", PTY: 0},
		FM: FMConfig{
			FrequencyMHz:        100.0,
			StereoEnabled:       true,
			PilotLevel:          0.09,
			RDSInjection:        0.04,
			PreEmphasisTauUS:    50,
			OutputDrive:         0.5,
			CompositeRateHz:     228000,
			MaxDeviationHz:      75000,
			LimiterEnabled:      true,
			LimiterCeiling:      1.0,
			FMModulationEnabled: true,
			MpxGain:             1.0,
		},
		Backend: BackendConfig{Kind: "file", OutputPath: "build/out/composite.f32"},
		Control: ControlConfig{ListenAddress: "127.0.0.1:8088"},
		Runtime: RuntimeConfig{FrameQueueCapacity: 3},
		Ingest: IngestConfig{
			Kind:           "none",
			PrebufferMs:    1500,
			StallTimeoutMs: 3000,
			Reconnect: IngestReconnectConfig{
				Enabled:          true,
				InitialBackoffMs: 1000,
				MaxBackoffMs:     15000,
			},
			Stdin: IngestPCMConfig{
				SampleRateHz: 44100,
				Channels:     2,
				Format:       "s16le",
			},
			HTTPRaw: IngestPCMConfig{
				SampleRateHz: 44100,
				Channels:     2,
				Format:       "s16le",
			},
			Icecast: IngestIcecastConfig{
				Decoder: "auto",
				RadioText: IngestIcecastRadioTextConfig{
					Enabled:      false,
					MaxLen:       64,
					OnlyOnChange: true,
				},
			},
			SRT: IngestSRTConfig{
				Mode:         "listener",
				SampleRateHz: 48000,
				Channels:     2,
			},
			AES67: IngestAES67Config{
				Discovery: IngestAES67DiscoveryConfig{
					TimeoutMs: 3000,
				},
				PayloadType:        97,
				SampleRateHz:       48000,
				Channels:           2,
				Encoding:           "L24",
				PacketTimeMs:       1,
				JitterDepthPackets: 8,
				ReadBufferBytes:    1 << 20,
			},
		},
	}
}

// ParsePI parses a hex PI code string. Returns an error for invalid input.
func ParsePI(pi string) (uint16, error) {
	trimmed := strings.TrimSpace(pi)
	if trimmed == "" {
		return 0, fmt.Errorf("rds.pi is required")
	}
	trimmed = strings.TrimPrefix(trimmed, "0x")
	trimmed = strings.TrimPrefix(trimmed, "0X")
	v, err := strconv.ParseUint(trimmed, 16, 16)
	if err != nil {
		return 0, fmt.Errorf("invalid rds.pi: %q", pi)
	}
	return uint16(v), nil
}

func Load(path string) (Config, error) {
	cfg := Default()
	if path == "" {
		return cfg, cfg.Validate()
	}
	data, err := os.ReadFile(path)
	if err != nil {
		return Config{}, err
	}
	if err := json.Unmarshal(data, &cfg); err != nil {
		return Config{}, err
	}
	return cfg, cfg.Validate()
}

func (c Config) Validate() error {
	if c.Audio.Gain < 0 || c.Audio.Gain > 4 {
		return fmt.Errorf("audio.gain out of range")
	}
	if c.Audio.ToneLeftHz <= 0 || c.Audio.ToneRightHz <= 0 {
		return fmt.Errorf("audio tone frequencies must be positive")
	}
	if c.Audio.ToneAmplitude < 0 || c.Audio.ToneAmplitude > 1 {
		return fmt.Errorf("audio.toneAmplitude out of range")
	}
	if c.FM.FrequencyMHz < 65 || c.FM.FrequencyMHz > 110 {
		return fmt.Errorf("fm.frequencyMHz out of range")
	}
	if c.FM.PilotLevel < 0 || c.FM.PilotLevel > 0.2 {
		return fmt.Errorf("fm.pilotLevel out of range")
	}
	if c.FM.RDSInjection < 0 || c.FM.RDSInjection > 0.15 {
		return fmt.Errorf("fm.rdsInjection out of range")
	}
	if c.FM.OutputDrive < 0 || c.FM.OutputDrive > 10 {
		return fmt.Errorf("fm.outputDrive out of range (0..10)")
	}
	if c.FM.CompositeRateHz < 96000 || c.FM.CompositeRateHz > 1520000 {
		return fmt.Errorf("fm.compositeRateHz out of range")
	}
	if c.FM.PreEmphasisTauUS < 0 || c.FM.PreEmphasisTauUS > 100 {
		return fmt.Errorf("fm.preEmphasisTauUS out of range (0=off, 50=EU, 75=US)")
	}
	if c.FM.MaxDeviationHz < 0 || c.FM.MaxDeviationHz > 150000 {
		return fmt.Errorf("fm.maxDeviationHz out of range")
	}
	if c.FM.LimiterCeiling < 0 || c.FM.LimiterCeiling > 2 {
		return fmt.Errorf("fm.limiterCeiling out of range")
	}
	if c.FM.MpxGain < 0.1 || c.FM.MpxGain > 5 {
		return fmt.Errorf("fm.mpxGain out of range (0.1..5)")
	}
	if c.Backend.Kind == "" {
		return fmt.Errorf("backend.kind is required")
	}
	if c.Backend.DeviceSampleRateHz < 0 {
		return fmt.Errorf("backend.deviceSampleRateHz must be >= 0")
	}
	if c.Control.ListenAddress == "" {
		return fmt.Errorf("control.listenAddress is required")
	}
	if c.Runtime.FrameQueueCapacity <= 0 {
		return fmt.Errorf("runtime.frameQueueCapacity must be > 0")
	}
	if c.Ingest.Kind == "" {
		c.Ingest.Kind = "none"
	}
	ingestKind := strings.ToLower(strings.TrimSpace(c.Ingest.Kind))
	switch ingestKind {
	case "none", "stdin", "stdin-pcm", "http-raw", "icecast", "srt", "aes67", "aoip", "aoip-rtp":
	default:
		return fmt.Errorf("ingest.kind unsupported: %s", c.Ingest.Kind)
	}
	if c.Ingest.PrebufferMs < 0 {
		return fmt.Errorf("ingest.prebufferMs must be >= 0")
	}
	if c.Ingest.StallTimeoutMs < 0 {
		return fmt.Errorf("ingest.stallTimeoutMs must be >= 0")
	}
	if c.Ingest.Reconnect.InitialBackoffMs < 0 || c.Ingest.Reconnect.MaxBackoffMs < 0 {
		return fmt.Errorf("ingest.reconnect backoff must be >= 0")
	}
	if c.Ingest.Reconnect.Enabled && c.Ingest.Reconnect.InitialBackoffMs <= 0 {
		return fmt.Errorf("ingest.reconnect.initialBackoffMs must be > 0 when reconnect is enabled")
	}
	if c.Ingest.Reconnect.Enabled && c.Ingest.Reconnect.MaxBackoffMs <= 0 {
		return fmt.Errorf("ingest.reconnect.maxBackoffMs must be > 0 when reconnect is enabled")
	}
	if c.Ingest.Reconnect.MaxBackoffMs > 0 && c.Ingest.Reconnect.InitialBackoffMs > c.Ingest.Reconnect.MaxBackoffMs {
		return fmt.Errorf("ingest.reconnect.initialBackoffMs must be <= maxBackoffMs")
	}
	if c.Ingest.Stdin.SampleRateHz <= 0 || c.Ingest.HTTPRaw.SampleRateHz <= 0 {
		return fmt.Errorf("ingest pcm sampleRateHz must be > 0")
	}
	if (c.Ingest.Stdin.Channels != 1 && c.Ingest.Stdin.Channels != 2) || (c.Ingest.HTTPRaw.Channels != 1 && c.Ingest.HTTPRaw.Channels != 2) {
		return fmt.Errorf("ingest pcm channels must be 1 or 2")
	}
	if strings.ToLower(strings.TrimSpace(c.Ingest.Stdin.Format)) != "s16le" || strings.ToLower(strings.TrimSpace(c.Ingest.HTTPRaw.Format)) != "s16le" {
		return fmt.Errorf("ingest pcm format must be s16le")
	}
	if ingestKind == "icecast" && strings.TrimSpace(c.Ingest.Icecast.URL) == "" {
		return fmt.Errorf("ingest.icecast.url is required when ingest.kind=icecast")
	}
	if ingestKind == "srt" && strings.TrimSpace(c.Ingest.SRT.URL) == "" {
		return fmt.Errorf("ingest.srt.url is required when ingest.kind=srt")
	}
	if ingestKind == "aes67" || ingestKind == "aoip" || ingestKind == "aoip-rtp" {
		hasSDP := strings.TrimSpace(c.Ingest.AES67.SDP) != ""
		hasSDPPath := strings.TrimSpace(c.Ingest.AES67.SDPPath) != ""
		discoveryEnabled := c.Ingest.AES67.Discovery.Enabled || strings.TrimSpace(c.Ingest.AES67.Discovery.StreamName) != ""
		if hasSDP && hasSDPPath {
			return fmt.Errorf("ingest.aes67.sdp and ingest.aes67.sdpPath are mutually exclusive")
		}
		if !hasSDP && !hasSDPPath {
			if strings.TrimSpace(c.Ingest.AES67.MulticastGroup) == "" && !discoveryEnabled {
				return fmt.Errorf("ingest.aes67.multicastGroup is required when ingest.kind=%s", ingestKind)
			}
			if (c.Ingest.AES67.Port <= 0 || c.Ingest.AES67.Port > 65535) && !discoveryEnabled {
				return fmt.Errorf("ingest.aes67.port must be 1..65535")
			}
		}
		if c.Ingest.AES67.Discovery.TimeoutMs < 0 {
			return fmt.Errorf("ingest.aes67.discovery.timeoutMs must be >= 0")
		}
		if c.Ingest.AES67.Discovery.SAPPort < 0 || c.Ingest.AES67.Discovery.SAPPort > 65535 {
			return fmt.Errorf("ingest.aes67.discovery.sapPort must be 0..65535")
		}
		if discoveryEnabled && strings.TrimSpace(c.Ingest.AES67.Discovery.StreamName) == "" {
			return fmt.Errorf("ingest.aes67.discovery.streamName is required when discovery is enabled")
		}
		if discoveryEnabled && c.Ingest.AES67.Port > 65535 {
			return fmt.Errorf("ingest.aes67.port must be 1..65535")
		}
		if c.Ingest.AES67.PayloadType < 0 || c.Ingest.AES67.PayloadType > 127 {
			return fmt.Errorf("ingest.aes67.payloadType must be 0..127")
		}
		if c.Ingest.AES67.SampleRateHz <= 0 {
			return fmt.Errorf("ingest.aes67.sampleRateHz must be > 0")
		}
		if c.Ingest.AES67.Channels != 1 && c.Ingest.AES67.Channels != 2 {
			return fmt.Errorf("ingest.aes67.channels must be 1 or 2")
		}
		if strings.ToUpper(strings.TrimSpace(c.Ingest.AES67.Encoding)) != "L24" {
			return fmt.Errorf("ingest.aes67.encoding must be L24")
		}
		if c.Ingest.AES67.PacketTimeMs <= 0 {
			return fmt.Errorf("ingest.aes67.packetTimeMs must be > 0")
		}
		if c.Ingest.AES67.JitterDepthPackets < 1 {
			return fmt.Errorf("ingest.aes67.jitterDepthPackets must be >= 1")
		}
		if c.Ingest.AES67.ReadBufferBytes < 0 {
			return fmt.Errorf("ingest.aes67.readBufferBytes must be >= 0")
		}
	}
	switch strings.ToLower(strings.TrimSpace(c.Ingest.SRT.Mode)) {
	case "", "listener", "caller", "rendezvous":
	default:
		return fmt.Errorf("ingest.srt.mode unsupported: %s", c.Ingest.SRT.Mode)
	}
	if c.Ingest.SRT.SampleRateHz <= 0 {
		return fmt.Errorf("ingest.srt.sampleRateHz must be > 0")
	}
	if c.Ingest.SRT.Channels != 1 && c.Ingest.SRT.Channels != 2 {
		return fmt.Errorf("ingest.srt.channels must be 1 or 2")
	}
	switch strings.ToLower(strings.TrimSpace(c.Ingest.Icecast.Decoder)) {
	case "", "auto", "native", "ffmpeg", "fallback":
	default:
		return fmt.Errorf("ingest.icecast.decoder unsupported: %s", c.Ingest.Icecast.Decoder)
	}
	if c.Ingest.Icecast.RadioText.MaxLen < 0 || c.Ingest.Icecast.RadioText.MaxLen > 64 {
		return fmt.Errorf("ingest.icecast.radioText.maxLen out of range (0-64)")
	}
	// Fail-loud PI validation
	if c.RDS.Enabled {
		if _, err := ParsePI(c.RDS.PI); err != nil {
			return fmt.Errorf("rds config: %w", err)
		}
	}
	if c.RDS.PTY < 0 || c.RDS.PTY > 31 {
		return fmt.Errorf("rds.pty out of range (0-31)")
	}
	if len(c.RDS.PS) > 8 {
		return fmt.Errorf("rds.ps must be <= 8 characters")
	}
	if len(c.RDS.RadioText) > 64 {
		return fmt.Errorf("rds.radioText must be <= 64 characters")
	}
	return nil
}

// EffectiveDeviceRate returns the device sample rate, falling back to composite rate.
func (c Config) EffectiveDeviceRate() float64 {
	if c.Backend.DeviceSampleRateHz > 0 {
		return c.Backend.DeviceSampleRateHz
	}
	return float64(c.FM.CompositeRateHz)
}
