#!/usr/bin/env python3
import argparse
import sys
import shlex
import subprocess
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / 'src' / 'full_repo_refactor'
sys.path.insert(0, str(ROOT / 'src'))

from full_repo_refactor.fixtures.fixture_manager import FixtureManager


def build_image(sandbox: str, features: list[str], env: dict[str, str], prefix: str | None):
    fixtures = FixtureManager(configs={})
    spec = fixtures.get_sandbox_spec(
        name=sandbox,
        features=features,
        environment=env,
        tempdir_prefix=f'cli_{sandbox}_',
    )

    compose_path = Path(spec.config)
    sandbox_dir = compose_path.parent

    cmd = [
        'docker', 'compose',
        '--project-directory', str(sandbox_dir),
        '-f', str(compose_path),
        'build'
    ]

    print("Building Full Repository Refactoring Docker image...")
    print(f"Context: {sandbox_dir}")
    print(f"Features: {','.join(features) if features else 'none'}")
    if env:
        print(f"Environment: {' '.join(f'{k}={v}' for k, v in env.items())}")
    print("")

    print(shlex.join(cmd))
    subprocess.check_call(cmd)

    print("")
    print("Docker image built successfully!")
    print("")
    print("To test the image:")
    print(f"  docker compose -f {compose_path} run --rm default bash")


def parse_env(items: list[str]) -> dict[str, str]:
    out: dict[str, str] = {}
    for item in items:
        if '=' not in item:
            raise SystemExit(f"--env must be KEY=VALUE, got: {item}")
        k, v = item.split('=', 1)
        out[k] = v
    return out


def main():
    p = argparse.ArgumentParser(description='Build Docker image with selected features and environment')
    p.add_argument('sandbox', help='Sandbox template name (e.g., default)')
    p.add_argument('--features', default='', help='Comma-separated list of features to enable')
    p.add_argument('--env', action='append', default=[], help='Environment var KEY=VALUE (repeatable)')
    p.add_argument('--prefix', default='', help='Prefix to add to image name (e.g., test_)')
    args = p.parse_args()

    features = [f.strip() for f in args.features.split(',') if f.strip()] if args.features else []
    env = parse_env(args.env)

    build_image(args.sandbox, features, env, args.prefix)

if __name__ == '__main__':
    main()
