import os
import typer
from .utils import (
    clear,
    check_internet_connection,
    is_youtube_video_id,
    validate_link,
    handle_video_link,
)
from .downloader import download


# Create CLI app
app = typer.Typer(
    name="pyutube",
    add_completion=False,
    help="Awesome CLI to download YouTube videos(as video or audio)/shorts from the terminal",
    rich_markup_mode="rich",
)


@app.command(
    name="download",
    help="Download a [red]YouTube[/red] videos (as video or audio), shorts, (playlists soon as possible).",
    epilog="Made with ❤️ By Ebraheem. Find me on GitHub: [link=https://github.com/Hetari]click here @Hetari[/link]"
)
def pyutube(
    url: str = typer.Argument(
        ...,
        help="YouTube URL",
        show_default=False),
    path: str = typer.Argument(
        os.getcwd(),
        help="Path to save video [default: <current directory>]",  show_default=False),
):
    """
    Downloads a YouTube video.

    Args:
        url (str): The URL of the YouTube video.
        path (str): The path to save the video. Defaults to the current working directory.

    Returns:
        None
    """
    clear()

    if not check_internet_connection():
        return

    if is_youtube_video_id(url):
        url = f"https://www.youtube.com/watch?v={url}"

    is_valid_link, link_type = validate_link(url)

    if not is_valid_link:
        return

    if link_type == "video":
        try:
            url, path, is_audio = handle_video_link(url, path)
        except TypeError:
            return

        download(url, path, is_audio)

    elif link_type == "short":
        download(url, path, is_audio)
