"""Application configuration class for ansible-creator."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from ansible_creator.utils import expand_path


if TYPE_CHECKING:
    from collections.abc import Sequence
    from pathlib import Path

    from ansible_creator.output import Output


@dataclass(frozen=True)
class Config:
    """The application configuration for ansible-creator.

    Attributes:
        creator_version: The version of ansible-creator.
        output: The output object to use for logging.
        subcommand: The subcommand to execute.
        collection: The collection name to scaffold.
        force: Whether to overwrite existing files.
        overwrite: To overwrite files in an existing directory.
        no_overwrite: To not overwrite files in an existing directory.
        init_path: The path to initialize the project.
        project: The type of project to scaffold.
        collection_name: The name of the collection.
        namespace: The namespace for the collection.
        resource_type: The type of resource to be scaffolded.
        plugin_name: The name of plugin to be scaffolded.
        plugin_type: The type of plugin to be scaffolded.
        type: The type of the project for which the resource is being scaffolded.
        path: The file path where the resource should be added.
        image: The image to be used while scaffolding devcontainer.
        role_name: The role to be scaffolded.
        skip_collection_check: Whether to skip collection path validation.
        ee_config: Inline JSON string containing EE parameters.
        ee_config_file: Path to a JSON/YAML config file for EE parameters.
        base_image: Base image for execution environment.
        ee_collections: List of Ansible collections for execution environment.
        ee_python_deps: List of Python dependencies for execution environment.
        ee_system_packages: List of system packages for execution environment.
        ee_name: Name/tag for the execution environment image.
        ee_file_name: Name of the EE definition file.
        registry_tls_verify: Whether to verify TLS for container registry operations
            (login, pull, push, and image builds). None means the user did not
            explicitly set this flag, so the EE config file value is preserved.
    """

    creator_version: str
    output: Output
    subcommand: str
    collection: str = ""
    force: bool = False
    overwrite: bool = False
    no_overwrite: bool = False
    init_path: str | Path = "./"
    project: str = ""
    collection_name: str | None = None
    namespace: str = ""
    resource_type: str = ""
    plugin_name: str = ""
    plugin_type: str = ""
    type: str = ""
    path: str | Path = "./"
    image: str = ""
    role_name: str = "run"
    skip_collection_check: bool = False
    ee_config: str | None = None
    ee_config_file: str | None = None
    base_image: str = "quay.io/fedora/fedora:41"
    ee_collections: Sequence[str] = field(default_factory=list)
    ee_python_deps: Sequence[str] = field(default_factory=list)
    ee_system_packages: Sequence[str] = field(default_factory=list)
    ee_name: str = "ansible_sample_ee"
    ee_file_name: str = "execution-environment.yml"
    registry_tls_verify: bool | None = None

    def __post_init__(self) -> None:
        """Post process config values."""
        if self.project == "ansible-project":
            object.__setattr__(self, "project", "playbook")

        if self.collection:
            fqcn = self.collection.split(".", maxsplit=1)
            object.__setattr__(self, "namespace", fqcn[0])
            object.__setattr__(self, "collection_name", fqcn[-1])

        if isinstance(self.init_path, str):
            object.__setattr__(self, "init_path", expand_path(self.init_path))

        if self.plugin_type == "module":
            object.__setattr__(self, "plugin_type", "modules")
