{% extends "base_config.html" %}
{% block content %}
<div class="page">
    <div class="section">
        <h2 class="section-title">
            <img class="card-icon" src="/static/assets/icons/github.svg" alt="Github">
            <span class="card-title-text">Github</span>
        </h2>
        <form>
            <table>
                <tr>
                    <td>
                        <label for="pat-token">Personal Access Token</label>
                    </td>
                    <td>
                        <input type="text" id="pat-token" name="pat" value="{{ current_config['pat_token'] }}">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="repo-owner">Repository Owner</label>
                    </td>
                    <td>
                        <input type="text" id="repo-owner" name="repo_owner" value="{{ current_config['repo_owner'] }}">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="repo-name">Repository Name</label>
                    </td>
                    <td>
                        <input type="text" id="repo-name" name="repo_name" value="{{ current_config['repo_name'] }}">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="repo-branch">Repository Branch</label>
                    </td>
                    <td>
                        <input type="text" id="repo-branch" name="repo_branch" value="{{ current_config['repo_branch'] }}">
                    </td>
                </tr>
            </table>
            <h4>You probably don't need to edit these.</h4>

            <table>
                <tr>
                    <td>
                        <label for="compressed-jsonl">Compressed JSONL (Output)</label>
                    </td>
                    <td>
                        <input type="text" id="compressed-jsonl" name="compressed-jsonl" value="{{ current_config['compressed_jsonl'] }}">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="embeddings-file">Embeddings File (Output)</label>
                    </td>
                    <td>
                        <input type="text" id="embeddings-file" name="embeddings-file" value="{{ current_config['embeddings_file'] }}">
                    </td>
                </tr>
            </table>
            <div class="section">
                <div id="success" style="display: none;"></div>
                <button id="submit" type="submit">Save</button>
            </div>
        </form>
    </div>
</div>
<script>
    submit.addEventListener("click", function(event) {
        event.preventDefault();

        var compressed_jsonl = document.getElementById("compressed-jsonl").value;
        var embeddings_file = document.getElementById("embeddings-file").value;
        var pat_token = document.getElementById("pat-token").value;
        var repo_owner = document.getElementById("repo-owner").value;
        var repo_name = document.getElementById("repo-name").value;
        var repo_branch = document.getElementById("repo-branch").value;

        fetch('/api/config/data/content_type/github', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                "pat_token": pat_token,
                "repo_owner": repo_owner,
                "repo_name": repo_name,
                "repo_branch": repo_branch,
                "compressed_jsonl": compressed_jsonl,
                "embeddings_file": embeddings_file,
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data["status"] == "ok") {
                document.getElementById("success").innerHTML = "✅ Successfully updated. Click Configure on your <a href='/config'>settings page</a> to complete your Khoj setup.";
                document.getElementById("success").style.display = "block";
            } else {
                document.getElementById("success").innerHTML = "⚠️ Failed to update settings.";
                document.getElementById("success").style.display = "block";
            }
        })
    });
</script>
{% endblock %}
