import gradio as gr
import os
import requests
import git
import tempfile
import shutil
from openai import OpenAI
import dotenv

def generate_prompt(repos_data, role, seniority, assignment_details, focus_areas=""):
    """Generates the AI analysis prompt with multiple repo details."""
    prompt = f"""
    You are an AI expert in evaluating software engineering candidates based on their GitHub repositories. Your goal is to assess the quality, organization, and best practices of the submitted code repositories. Below is the candidate's information:

    ### Candidate Information:
    - **Role Applied For**: {role}
    - **Seniority Level**: {seniority}

    ### Guidelines:
    - Focus on the candidate's ability to write clean, efficient, and maintainable code.
    - Consider the seniority level when evaluating the code.
    - Consider the assignment details and how the candidate responded.

    ### Assignment Details:
    {assignment_details}

    ### Repository Analysis:
    Below are the repositories submitted by the candidate:
    {repos_data}

    ### Evaluation Criteria:
    1. **Code Organization & Architecture**
    2. **Code Quality & Best Practices**
    3. **Language Proficiency & Best Practices**
    4. **Use of Frameworks & Libraries**

    ### Optional Key Focus Areas:
    {focus_areas}

    ### **Final Score Calculation**
    - Score each repository **out of 100** and provide an overall weighted score.
    - Justify the **score** by explaining the candidate’s strengths and weaknesses.

    ### **Expected Output:**
    - **Per Repository Analysis**: A detailed breakdown of strengths and weaknesses.
    - **Overall Candidate Summary**: A final evaluation of their coding skills across all repositories.
    - **Final Score (0-100)**: A numeric score with justification.
    """
    return prompt

# Load environment variables
dotenv.load_dotenv()
client = OpenAI()

PASSWORD = os.getenv("PASSWORD", "defaultpass")

def authenticate(password):
    """Check if the entered password is correct."""
    if password != PASSWORD:
        return "❌ Incorrect password! Access denied.", None
    return None, "βœ… Access granted! You may proceed."

def analyze_repos(repo_urls, role, seniority, assignment_details, focus_areas, password):
    auth_error, auth_success = authenticate(password)
    if auth_error:
        return auth_error, gr.update(visible=False)

    """Clone and analyze multiple GitHub repositories, supporting specific branches."""
    repo_urls = [url.strip() for url in repo_urls.split(",") if url.strip()]
    
    if not all(url.startswith("https://github.com/") for url in repo_urls):
        return "❌ One or more URLs are invalid!", gr.update(visible=False)

    temp_dir = tempfile.mkdtemp()
    repos_data = ""
    total_files = 0

    try:
        progress = gr.update(value="πŸ”„ Cloning repositories...", visible=True)
        
        for repo_url in repo_urls:
            # Extract branch if provided (URLs with "/tree/")
            branch = None
            if "/tree/" in repo_url:
                repo_url, branch = repo_url.split("/tree/")[:2]
                repo_url = repo_url.rstrip("/")  # Clean base repo URL
            
            repo_name = repo_url.split("/")[-1]
            repo_path = os.path.join(temp_dir, repo_name)

            try:
                repo = git.Repo.clone_from(repo_url + ".git", repo_path)
                if branch:
                    repo.git.checkout(branch)
            except Exception as e:
                repos_data += f"\n❌ Failed to clone `{repo_name}` (Branch: `{branch or 'default'}`): {str(e)}\n"
                continue

            repo_data = f"\nπŸ“‚ **Repository: {repo_name}** (Branch: `{branch or 'default'}`)\n"
            file_count = 0  

            for root, _, filenames in os.walk(repo_path):
                for file in filenames:
                    file_count += 1  
                    file_path = os.path.join(root, file)

                    try:
                        with open(file_path, "r", encoding="utf-8") as f:
                            file_content = f.read()
                        repo_data += f"\n**File {file_count}:** {file_path.replace(repo_path, '')}\n```\n{file_content[:1000]}\n```\n"
                    except Exception:
                        repo_data += f"\n**File {file_count}:** {file_path.replace(repo_path, '')} (⚠️ Cannot read binary file)\n"

            repos_data += repo_data
            total_files += file_count

        progress = gr.update(value="πŸ€– Sending data to AI for evaluation...", visible=True)

        # AI-based evaluation
        evaluation = f"βœ… **Evaluation for Role: {role}**\n\n"
        evaluation += f"πŸ“‚ `{len(repo_urls)}` repositories analyzed, containing `{total_files}` files.\n"
        evaluation += f"πŸ’‘ Key focus areas: {focus_areas}\n\n"
        evaluation += "**πŸ” Code Quality Analysis:**\n"
        
        completion = client.chat.completions.create(
            model="o1",
            messages=[
                {
                    "role": "user",
                    "content": generate_prompt(repos_data, role, seniority, assignment_details, focus_areas)
                }
            ]
        )
        
        evaluation += "\n" + completion.choices[0].message.content

        progress = gr.update(value="βœ… Analysis complete!", visible=True)
        return evaluation, progress

    except Exception as e:
        return f"❌ Error analyzing repositories: {str(e)}", gr.update(visible=False)
    
    finally:
        shutil.rmtree(temp_dir)  # Cleanup
        
# Gradio UI
with gr.Blocks() as app:
    gr.Markdown("# πŸ› οΈ AI-Powered Candidate Evaluation System")

    password = gr.Textbox(label="Enter Password")
    role = gr.Textbox(label="Role the Candidate is Applying For")
    seniority = gr.Dropdown(
        ["Junior", "Mid", "Senior"],
        label="Seniority Level",
        value="Mid"
    )
    assignment_details = gr.Textbox(label="Assignment Details", lines=8)
    repo_urls = gr.Textbox(label="GitHub Repository URLs (comma-separated)")
    focus_areas = gr.Textbox(label="Optional Focus Areas (e.g., Clean Code, Performance)")

    output = gr.Markdown()
    progress = gr.Markdown(visible=False)  # Loader

    submit_btn = gr.Button("πŸ” Evaluate")

    submit_btn.click(
        fn=analyze_repos,
        inputs=[repo_urls, role, seniority, assignment_details, focus_areas, password],
        outputs=[output, progress]
    )

app.launch()