"""Command-line interface options for the readme-ai application."""

from __future__ import annotations

import os
from typing import Optional

import click

from readmeai.config.enums import BadgeOptions, ImageOptions


def prompt_for_image(
    context: Optional[click.Context],
    parameter: Optional[click.Parameter],
    value: Optional[str],
) -> str:
    """Prompt the user for a custom image URL."""
    if value == ImageOptions.CUSTOM.name:
        return click.prompt("Provide an image URL: ")
    elif value in ImageOptions.__members__:
        return ImageOptions[value].value
    else:
        raise click.BadParameter(f"Invalid image URL provided: {value}")


align = click.option(
    "-a",
    "--align",
    type=click.Choice(["center", "left"], case_sensitive=False),
    default="center",
    help="Align the text in the README.md file's header to the left or center.",
)

api_key = click.option(
    "--api-key",
    default=os.environ.get("OPENAI_API_KEY", None),
    help="LLM API key to generate text for the README.md file.",
)

badges = click.option(
    "-b",
    "--badges",
    type=click.Choice(
        [opt.value for opt in BadgeOptions], case_sensitive=False
    ),
    default=BadgeOptions.DEFAULT.value,
    help="""\
        Badge icon style types to select from when generating README.md badges. The following options are currently available:\n
        - default \n
        - flat \n
        - flat-square \n
        - for-the-badge \n
        - plastic \n
        - skills \n
        - skills-light \n
        - social \n
        """,
)

badge_color = click.option(
    "--badge-color",
    type=str,
    default="0080ff",
    help="Custom color for the badge icon. Provide a valid color name or hex code.",
)

emojis = click.option(
    "-e",
    "--emojis",
    is_flag=True,
    default=False,
    help="This option adds emojis to the README.md file's header sections. For example, the default header for the 'Overview' section generates the markdown code as '## Overview'. Adding the --emojis flag generates the markdown code as '## 📍 Overview'.",
)

image = click.option(
    "-i",
    "--image",
    type=click.Choice(
        [opt.name for opt in ImageOptions], case_sensitive=False
    ),
    default=ImageOptions.BLUE.name,
    callback=prompt_for_image,
    show_choices=True,
    help="""\
        Project logo image displayed in the README file header. Choose from the following options:\n
        - CUSTOM \n
        - BLACK \n
        - BLUE \n
        - CLOUD \n
        - GRADIENT \n
        - GREY \n
        - PURPLE \n
        """,
)

language = click.option(
    "-l",
    "--language",
    default="en",
    help="Language to use for generating the README.md file. Default is English (en).",
)

max_tokens = click.option(
    "--max-tokens",
    default=3899,
    type=int,
    help="Maximum number of tokens to generate for each section of the README.md file.",
)

model = click.option(
    "-m",
    "--model",
    default="gpt-3.5-turbo",
    help="GPT language model to use for generating various sections of the README.md file.",
)

offline = click.option(
    "--offline",
    is_flag=True,
    default=False,
    help="Use this option to generate a README.md file for free without making any API calls! This option generates the same README file structure, and leaves placeholders for content thats generated by the LLM, which include the project slogan inserted in the header section, along with the 'Overview', 'Features', and 'Modules' sections.",
)

output = click.option(
    "-o",
    "--output",
    default="readme-ai.md",
    help="Output file name for your README file. Default name is 'readme-ai.md'.",
)

repository = click.option(
    "-r",
    "--repository",
    required=True,
    help="Provide a remote repository URL (GitHub, GitLab, BitBucket), or a local directory path to your project.",
)

temperature = click.option(
    "-t",
    "--temperature",
    default=1.0,
    type=click.FloatRange(0.0, 2.0, clamp=True),
    help="Setting the model's temperature to a higher value will yield more creative content generated, while a lower value will generate more predictable content.",
)

template = click.option(
    "--template",
    type=str,
    help="README template file to use for generating the README.md file.",
)

tree_depth = click.option(
    "--tree-depth",
    default=5,
    type=int,
    help="Maximum depth of the directory tree thats included in the README.md file.",
)

vertex_ai = click.option(
    "--vertex_ai",
    nargs=2,
    type=str,
    default=(None, None),
    help="""Google Vertex AI configuration. Requires two arguments:
        1) VertexAI location i.e. us-central1
        2) VertexAI project ID i.e. my-project-id

        Example usage: --vertexai us-central1 my-project-id
    """,
)
