/**
 * Shared TypeScript interfaces matching the backend Pydantic API schemas.
 *
 * These mirror the JSON shapes returned by the REST API. Field names use
 * snake_case to match the JSON payloads — no camelCase transformation is done.
 */

// ── Device ────────────────────────────────────────────────────

export type DeviceType =
    | 'wled' | 'adalight' | 'ambiled' | 'mock' | 'mqtt' | 'ws'
    | 'openrgb' | 'dmx' | 'espnow' | 'hue' | 'usbhid' | 'spi'
    | 'chroma' | 'gamesense';

export interface Device {
    id: string;
    name: string;
    url: string;
    device_type: DeviceType;
    led_count: number;
    enabled: boolean;
    baud_rate?: number;
    auto_shutdown: boolean;
    send_latency_ms: number;
    rgbw: boolean;
    zone_mode: string;
    capabilities: string[];
    tags: string[];
    dmx_protocol: string;
    dmx_start_universe: number;
    dmx_start_channel: number;
    espnow_peer_mac: string;
    espnow_channel: number;
    hue_username: string;
    hue_client_key: string;
    hue_entertainment_group_id: string;
    spi_speed_hz: number;
    spi_led_type: string;
    chroma_device_type: string;
    gamesense_device_type: string;
    default_css_processing_template_id: string;
    created_at: string;
    updated_at: string;
}

// ── Output Target ─────────────────────────────────────────────

export type TargetType = 'led' | 'key_colors';

export interface KeyColorsSettings {
    fps: number;
    interpolation_mode: string;
    smoothing: number;
    pattern_template_id: string;
    brightness: number;
    brightness_value_source_id: string;
}

export interface OutputTarget {
    id: string;
    name: string;
    target_type: TargetType;
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;

    // LED target fields
    device_id?: string;
    color_strip_source_id?: string;
    brightness_value_source_id?: string;
    fps?: number;
    keepalive_interval?: number;
    state_check_interval?: number;
    min_brightness_threshold?: number;
    adaptive_fps?: boolean;
    protocol?: string;

    // Key Colors target fields
    picture_source_id?: string;
    key_colors_settings?: KeyColorsSettings;
}

// ── Color Strip Source ────────────────────────────────────────

export type CSSSourceType =
    | 'picture' | 'picture_advanced' | 'static' | 'gradient'
    | 'color_cycle' | 'effect' | 'composite' | 'mapped'
    | 'audio' | 'api_input' | 'notification' | 'daylight'
    | 'candlelight' | 'processed';

export interface ColorStop {
    position: number;
    color: number[];
    color_right?: number[];
}

export interface CompositeLayer {
    source_id: string;
    blend_mode: string;
    opacity: number;
    enabled: boolean;
    brightness_source_id?: string;
    processing_template_id?: string;
}

export interface MappedZone {
    source_id: string;
    start: number;
    end: number;
    reverse: boolean;
}

export interface AnimationConfig {
    enabled: boolean;
    type: string;
    speed: number;
}

export interface CalibrationLine {
    picture_source_id: string;
    edge: 'top' | 'right' | 'bottom' | 'left';
    led_count: number;
    span_start: number;
    span_end: number;
    reverse: boolean;
    border_width: number;
}

export interface Calibration {
    mode: 'simple' | 'advanced';
    lines?: CalibrationLine[];
    layout?: 'clockwise' | 'counterclockwise';
    start_position?: 'top_left' | 'top_right' | 'bottom_left' | 'bottom_right';
    offset?: number;
    leds_top?: number;
    leds_right?: number;
    leds_bottom?: number;
    leds_left?: number;
    span_top_start?: number;
    span_top_end?: number;
    span_right_start?: number;
    span_right_end?: number;
    span_bottom_start?: number;
    span_bottom_end?: number;
    span_left_start?: number;
    span_left_end?: number;
    skip_leds_start?: number;
    skip_leds_end?: number;
    border_width?: number;
}

export interface ColorStripSource {
    id: string;
    name: string;
    source_type: CSSSourceType;
    led_count: number;
    description?: string;
    tags: string[];
    overlay_active: boolean;
    clock_id?: string;
    created_at: string;
    updated_at: string;

    // Picture
    picture_source_id?: string;
    smoothing?: number;
    interpolation_mode?: string;
    calibration?: Calibration;

    // Static
    color?: number[];

    // Gradient
    stops?: ColorStop[];

    // Color cycle
    colors?: number[][];

    // Effect
    effect_type?: string;
    palette?: string;
    intensity?: number;
    scale?: number;
    mirror?: boolean;

    // Composite
    layers?: CompositeLayer[];

    // Mapped
    zones?: MappedZone[];

    // Audio
    visualization_mode?: string;
    audio_source_id?: string;
    sensitivity?: number;
    color_peak?: number[];

    // Animation
    animation?: AnimationConfig;
    speed?: number;

    // API Input
    fallback_color?: number[];
    timeout?: number;
    interpolation?: string;

    // Notification
    notification_effect?: string;
    duration_ms?: number;
    default_color?: string;
    app_colors?: Record<string, string>;
    app_filter_mode?: string;
    app_filter_list?: string[];
    os_listener?: boolean;

    // Daylight
    use_real_time?: boolean;
    latitude?: number;

    // Candlelight
    num_candles?: number;

    // Processed
    input_source_id?: string;
    processing_template_id?: string;

    // Weather
    weather_source_id?: string;
    temperature_influence?: number;
}

// ── Pattern Template ──────────────────────────────────────────

export interface KeyColorRectangle {
    name: string;
    x: number;
    y: number;
    width: number;
    height: number;
}

export interface PatternTemplate {
    id: string;
    name: string;
    rectangles: KeyColorRectangle[];
    tags: string[];
    description?: string;
    created_at: string;
    updated_at: string;
}

// ── Value Source ───────────────────────────────────────────────

export type ValueSourceType =
    | 'static' | 'animated' | 'audio'
    | 'adaptive_time' | 'adaptive_scene' | 'daylight';

export interface SchedulePoint {
    time: string;
    value: number;
}

export interface ValueSource {
    id: string;
    name: string;
    source_type: ValueSourceType;
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;

    // Static
    value?: number;

    // Animated
    waveform?: string;
    speed?: number;
    min_value?: number;
    max_value?: number;

    // Audio
    audio_source_id?: string;
    mode?: string;
    sensitivity?: number;
    smoothing?: number;
    auto_gain?: boolean;

    // Adaptive
    schedule?: SchedulePoint[];
    picture_source_id?: string;
    scene_behavior?: string;

    // Daylight
    use_real_time?: boolean;
    latitude?: number;
}

// ── Audio Source ───────────────────────────────────────────────

export interface AudioSource {
    id: string;
    name: string;
    source_type: 'multichannel' | 'mono' | 'band_extract';
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;

    // Multichannel
    device_index?: number;
    is_loopback?: boolean;
    audio_template_id?: string;

    // Mono
    audio_source_id?: string;
    channel?: string;

    // Band Extract
    band?: string;
    freq_low?: number;
    freq_high?: number;
}

// ── Picture Source ─────────────────────────────────────────────

export type PictureSourceType = 'raw' | 'processed' | 'static_image' | 'video';

export interface PictureSource {
    id: string;
    name: string;
    stream_type: PictureSourceType;
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;

    // Raw
    display_index?: number;
    capture_template_id?: string;
    target_fps?: number;

    // Processed
    source_stream_id?: string;
    postprocessing_template_id?: string;

    // Static image
    image_asset_id?: string;

    // Video
    video_asset_id?: string;
    loop?: boolean;
    playback_speed?: number;
    start_time?: number;
    end_time?: number;
    resolution_limit?: number;
    clock_id?: string;
}

// ── Scene Preset ──────────────────────────────────────────────

export interface TargetSnapshot {
    id?: string;
    target_id: string;
    running: boolean;
    color_strip_source_id: string;
    brightness_value_source_id: string;
    fps: number;
}

export interface ScenePreset {
    id: string;
    name: string;
    description: string;
    color?: string;
    targets: TargetSnapshot[];
    order: number;
    tags: string[];
    created_at: string;
    updated_at: string;
}

// ── Sync Clock ────────────────────────────────────────────────

export interface SyncClock {
    id: string;
    name: string;
    speed: number;
    description?: string;
    tags: string[];
    is_running: boolean;
    elapsed_time: number;
    created_at: string;
    updated_at: string;
}

export interface WeatherSource {
    id: string;
    name: string;
    provider: string;
    provider_config: Record<string, any>;
    latitude: number;
    longitude: number;
    update_interval: number;
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;
}

export interface WeatherSourceListResponse {
    sources: WeatherSource[];
    count: number;
}

// ── Home Assistant Source ────────────────────────────────────

export interface HomeAssistantSource {
    id: string;
    name: string;
    host: string;
    use_ssl: boolean;
    entity_filters: string[];
    connected: boolean;
    entity_count: number;
    description?: string;
    tags: string[];
    created_at: string;
    updated_at: string;
}

export interface HomeAssistantSourceListResponse {
    sources: HomeAssistantSource[];
    count: number;
}

// ── Asset ────────────────────────────────────────────────────

export interface Asset {
    id: string;
    name: string;
    filename: string;
    mime_type: string;
    asset_type: string;
    size_bytes: number;
    description?: string;
    tags: string[];
    prebuilt: boolean;
    created_at: string;
    updated_at: string;
}

export interface AssetListResponse {
    assets: Asset[];
    count: number;
}

// ── Automation ────────────────────────────────────────────────

export type ConditionType =
    | 'always' | 'application' | 'time_of_day' | 'system_idle'
    | 'display_state' | 'mqtt' | 'webhook' | 'startup';

export interface AutomationCondition {
    condition_type: ConditionType;
    apps?: string[];
    match_type?: string;
    start_time?: string;
    end_time?: string;
    idle_minutes?: number;
    when_idle?: boolean;
    state?: string;
    topic?: string;
    payload?: string;
    match_mode?: string;
    token?: string;
}

export interface Automation {
    id: string;
    name: string;
    enabled: boolean;
    condition_logic: 'or' | 'and';
    conditions: AutomationCondition[];
    scene_preset_id?: string;
    deactivation_mode: 'none' | 'revert' | 'fallback_scene';
    deactivation_scene_preset_id?: string;
    tags: string[];
    webhook_url?: string;
    is_active: boolean;
    last_activated_at?: string;
    last_deactivated_at?: string;
    created_at: string;
    updated_at: string;
}

// ── Templates ─────────────────────────────────────────────────

export interface FilterInstance {
    filter_id: string;
    options: Record<string, any>;
}

export interface CaptureTemplate {
    id: string;
    name: string;
    engine_type: string;
    engine_config: Record<string, any>;
    tags: string[];
    description?: string;
    created_at: string;
    updated_at: string;
}

export interface PostprocessingTemplate {
    id: string;
    name: string;
    filters: FilterInstance[];
    tags: string[];
    description?: string;
    created_at: string;
    updated_at: string;
}

export interface ColorStripProcessingTemplate {
    id: string;
    name: string;
    filters: FilterInstance[];
    tags: string[];
    description?: string;
    created_at: string;
    updated_at: string;
}

export interface AudioTemplate {
    id: string;
    name: string;
    engine_type: string;
    engine_config: Record<string, any>;
    tags: string[];
    description?: string;
    created_at: string;
    updated_at: string;
}

// ── Filter Definition (from /filters endpoint) ────────────────

export interface FilterOptionDef {
    type: string;
    default?: any;
    min?: number;
    max?: number;
    step?: number;
    choices?: string[];
    label?: string;
}

export interface FilterDef {
    id: string;
    name: string;
    description?: string;
    category?: string;
    options: Record<string, FilterOptionDef>;
}

// ── Engine Info (from /capture-engines, /audio-engines) ───────

export interface EngineInfo {
    type: string;
    name: string;
    available: boolean;
    has_own_displays?: boolean;
    default_config?: Record<string, any>;
}

// ── Display ───────────────────────────────────────────────────

export interface Display {
    index: number;
    name: string;
    width: number;
    height: number;
    x: number;
    y: number;
    is_primary: boolean;
}

// ── API List Response Wrappers ────────────────────────────────

export interface DeviceListResponse {
    devices: Device[];
    count: number;
}

export interface OutputTargetListResponse {
    targets: OutputTarget[];
    count: number;
}

export interface ColorStripSourceListResponse {
    sources: ColorStripSource[];
    count: number;
}

export interface PatternTemplateListResponse {
    templates: PatternTemplate[];
    count: number;
}

export interface ValueSourceListResponse {
    sources: ValueSource[];
    count: number;
}

export interface AudioSourceListResponse {
    sources: AudioSource[];
    count: number;
}

export interface PictureSourceListResponse {
    streams: PictureSource[];
    count: number;
}

export interface ScenePresetListResponse {
    presets: ScenePreset[];
    count: number;
}

export interface SyncClockListResponse {
    clocks: SyncClock[];
    count: number;
}

export interface AutomationListResponse {
    automations: Automation[];
    count: number;
}

// ── Component Option Types (re-exported from authoritative sources) ───

export type { IconSelectItem, IconSelectOpts } from './core/icon-select.ts';
export type { EntitySelectOpts } from './core/entity-palette.ts';
export type { BulkAction, CardItem, CardSectionOpts } from './core/card-sections.ts';
export type { FilterListOpts } from './core/filter-list.ts';
