# -*- coding: utf-8 -*-

from typing import List, get_args

from fastapi import APIRouter, Body, HTTPException, Path
from ...config import (
    load_config,
    save_config,
    ChannelConfig,
    ChannelConfigUnion,
)

from ..channels.schema import ChannelType

router = APIRouter(prefix="/config", tags=["config"])


@router.get(
    "/channels",
    response_model=ChannelConfig,
    summary="List all channels",
    description="Retrieve configuration for all available channels",
)
async def list_channels() -> ChannelConfig:
    """List all channel configs."""
    config = load_config()
    return config.channels


@router.get(
    "/channels/types",
    response_model=List[ChannelType],
    summary="List channel types",
    description="Return all available channel type identifiers",
)
async def list_channel_types() -> List[ChannelType]:
    """Return available ChannelType values (derived from ChannelConfig)."""
    return list(get_args(ChannelType))


@router.put(
    "/channels",
    response_model=ChannelConfig,
    summary="Update all channels",
    description="Update configuration for all channels at once",
)
async def put_channels(
    channels_config: ChannelConfig = Body(
        ...,
        description="Complete channel configuration",
    ),
) -> ChannelConfig:
    """Update all channel configs."""
    config = load_config()
    config.channels = channels_config
    save_config(config)
    return channels_config


@router.get(
    "/channels/{channel_name}",
    response_model=ChannelConfigUnion,
    summary="Get channel config",
    description="Retrieve configuration for a specific channel by name",
)
async def get_channel(
    channel_name: str = Path(
        ...,
        description="Name of the channel to retrieve",
        min_length=1,
    ),
) -> ChannelConfigUnion:
    """Get a specific channel config by name."""
    config = load_config()
    single_channel_config = getattr(config.channels, channel_name, None)
    if single_channel_config is None:
        raise HTTPException(
            status_code=404,
            detail=f"Channel '{channel_name}' not found",
        )
    return single_channel_config


@router.put(
    "/channels/{channel_name}",
    response_model=ChannelConfigUnion,
    summary="Update channel config",
    description="Update configuration for a specific channel by name",
)
async def put_channel(
    channel_name: str = Path(
        ...,
        description="Name of the channel to update",
        min_length=1,
    ),
    single_channel_config: ChannelConfigUnion = Body(
        ...,
        description="Updated channel configuration",
    ),
) -> ChannelConfigUnion:
    """Update a specific channel config by name."""
    config = load_config()

    if not hasattr(config.channels, channel_name):
        raise HTTPException(
            status_code=404,
            detail=f"Channel '{channel_name}' not found",
        )

    setattr(config.channels, channel_name, single_channel_config)
    save_config(config)
    return single_channel_config
