import gradio as gr
import subprocess
import tempfile
import os
import shutil
from urllib.parse import urlparse

def validate_repo_url(url):
    if not url:
        return False
    if '/' in url and len(url.split('/')) == 2:
        return True
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc])
    except:
        return False

def create_download_file(content):
    if not content:
        return None
    try:
        temp_file = tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt')
        temp_file.write(content)
        temp_file.close()
        return temp_file.name
    except Exception as e:
        print(f"Error creating download file: {str(e)}")
        return None

def pack_repository(repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check):
    if not repo_url:
        return "Error: Please provide a repository URL", None
        
    if not validate_repo_url(repo_url):
        return "Error: Invalid repository URL format", None
        
    temp_dir = None
    try:
        temp_dir = tempfile.mkdtemp()
        cmd = ["npx", "repomix"]
        cmd.extend(["--remote", repo_url])
        
        if branch:
            cmd.extend(["--remote-branch", branch])
            
        if gh_token:
            cmd.extend(["--token", gh_token])
            
        cmd.extend(["--style", output_style])
        
        if remove_comments:
            cmd.append("--remove-comments")
        if remove_empty_lines:
            cmd.append("--remove-empty-lines")
        if not security_check:
            cmd.append("--no-security-check")
            
        output_file = os.path.join(temp_dir, "repomix-output.txt")
        cmd.extend(["-o", output_file])
        
        result = subprocess.run(
            cmd, 
            capture_output=True, 
            text=True,
            cwd=temp_dir
        )
        
        if result.returncode != 0:
            return f"Error running Repomix: {result.stderr}", None
            
        if not os.path.exists(output_file):
            return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}", None
            
        try:
            with open(output_file, 'r', encoding='utf-8') as f:
                content = f.read()
            
            download_path = create_download_file(content)
            return content, download_path
            
        except Exception as e:
            return f"Error reading output file: {str(e)}", None
            
    except Exception as e:
        return f"Error: {str(e)}", None
        
    finally:
        if temp_dir and os.path.exists(temp_dir):
            try:
                shutil.rmtree(temp_dir)
            except Exception as e:
                print(f"Warning: Could not remove temporary directory: {str(e)}")

with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
    with gr.Row():
        with gr.Column():
            repo_url = gr.Textbox(
                label="Repository URL or GitHub Shorthand",
                placeholder="e.g., https://github.com/user/repo or user/repo"
            )
            branch = gr.Textbox(
                label="Branch/Tag/Commit (optional)",
                placeholder="e.g., main, master, v1.0.0"
            )
            gh_token = gr.Textbox(
                label="GitHub Token (for private repos)",
                placeholder="ghp_xxxxxxxxxxxxxx",
                type="password"
            )
            
            with gr.Row():
                output_style = gr.Radio(
                    choices=["plain", "xml", "markdown"],
                    value="plain",
                    label="Output Style"
                )
            
            with gr.Row():
                remove_comments = gr.Checkbox(
                    label="Remove Comments",
                    value=False
                )
                remove_empty_lines = gr.Checkbox(
                    label="Remove Empty Lines",
                    value=False
                )
                security_check = gr.Checkbox(
                    label="Enable Security Check",
                    value=True
                )
            
            pack_button = gr.Button("Pack Repository", variant="primary")
    
    with gr.Row():
        output_text = gr.TextArea(
            label="Output",
            placeholder="Packed repository content will appear here...",
            lines=20,
            show_copy_button=True
        )
        
    with gr.Row():
        download_button = gr.File(
            label="Download Output",
            file_count="single",
            type="filepath",
            interactive=False
        )
    
    pack_button.click(
        fn=pack_repository,
        inputs=[repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check],
        outputs=[output_text, download_button]
    )

if __name__ == "__main__":
    demo.launch()