#!/usr/bin/env python

# WIP
# TODO clean this up

"""
Basic configure script for new users
"""

import fileinput
import subprocess
import sys

from rich.prompt import Confirm, Prompt

upstream_config = {
    "seed_repo": "https://github.com/khuedoan/homelab",
    "domain": "khuedoan.com",
    "timezone": "Asia/Ho_Chi_Minh",
    "terraform_workspace": "khuedoan",
    "loadbalancer_ip_range": "192.168.1.224/27",
}


def check_python_version(required_version: str) -> None:
    if sys.version_info < tuple(map(int, required_version.split('.'))):
        raise Exception(f"Must be using Python >= {required_version}")


def find_and_replace(pattern: str, replacement: str, paths: list[str]) -> None:
    files_with_matches = subprocess.run(
        ["git", "grep", "--files-with-matches", pattern, "--"] + paths,
        capture_output=True,
        text=True
    ).stdout.splitlines()

    for file_with_maches in files_with_matches:
        with fileinput.FileInput(file_with_maches, inplace=True) as file:
            for line in file:
                print(line.replace(pattern, replacement), end='')


def main() -> None:
    check_python_version(
        required_version='3.10.0'
    )

    editor = Prompt.ask("Select text editor", default='nvim')
    domain = Prompt.ask("Enter your domain", default=upstream_config['domain'])
    seed_repo = Prompt.ask("Enter seed repo", default=upstream_config['seed_repo'])
    timezone = Prompt.ask("Enter time zone", default=upstream_config['timezone'])
    loadbalancer_ip_range = Prompt.ask("Enter IP range for load balancer", default=upstream_config['loadbalancer_ip_range'])

    find_and_replace(
        pattern=upstream_config['domain'],
        replacement=domain,
        paths=[
            ".ci",
            "apps",
            "platform",
            "system",
            "external"
        ]
    )

    find_and_replace(
        pattern=upstream_config['seed_repo'],
        replacement=seed_repo,
        paths=[
            "system",
            "platform"
        ]
    )

    find_and_replace(
        pattern=upstream_config['timezone'],
        replacement=timezone,
        paths=[
            "apps",
            "system",
            "metal"
        ]
    )

    find_and_replace(
        pattern=upstream_config['loadbalancer_ip_range'],
        replacement=loadbalancer_ip_range,
        paths=[
            "metal/inventories/prod.yml",
            "apps/tailscale/values.yaml",
        ]
    )

    if Confirm.ask("Update server list?", default=True):
        subprocess.run(
            [editor, 'metal/inventories/prod.yml']
        )


    if Confirm.ask("Do you want to use managed services?"):
        terraform_workspace = Prompt.ask("Enter Terraform Workspace", default=upstream_config['terraform_workspace'])

        find_and_replace(
            pattern=upstream_config['terraform_workspace'],
            replacement=terraform_workspace,
            paths=[
                "external/versions.tf"
            ]
        )


if __name__ == '__main__':
    main()