<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<style>
    body {
        background-color: #f8f9fa;
    }
    .commit-card {
        transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
        border-left: 4px solid #0d6efd; /* Aksen warna biru di kiri */
    }
    .commit-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 .5rem 1rem rgba(0,0,0,.1)!important;
    }
    .avatar {
        width: 48px;
        height: 48px;
        object-fit: cover;
    }
    .commit-message {
        font-size: 1.1rem;
        font-weight: 600;
        color: #212529;
        text-decoration: none;
    }
    .commit-message:hover {
        text-decoration: underline;
        color: #0d6efd;
    }
    .file-list {
        max-height: 200px;
        overflow-y: auto;
    }
</style>
<div class="page-wrapper">
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Commit History</h4>
			</div>
		</div>
        
        <div class="row mb-4 bg-light p-3 rounded shadow-sm">
            <div class="col-md-3">
                <label class="form-label fw-bold">1. Select User</label>
                <select id="selectUser" class="form-select">
                    <option value="">-- Select User --</option>
                    <option value="alamdh22">alamdh22</option>
                    <option value="faiztyanirh">faiztyanirh</option>
                    <option value="mahdahar">mahdahar</option>
                    <option value="mikael-zakaria">mikael-zakaria</option>
                </select>
            </div>
            
            <div class="col-md-3">
                <label class="form-label fw-bold">2. Select Repository</label>
                <select id="selectRepo" class="form-select" disabled>
                    <option value="">-- Select Repository --</option>
                </select>
            </div>
            
            <div class="col-md-3">
                <label class="form-label fw-bold">3. Select Branch</label>
                <select id="selectBranch" class="form-select" disabled>
                    <option value="">-- Select Branch --</option>
                </select>
            </div>

            <div class="col-md-3 d-flex align-items-end">
                <button id="btnLoadCommits" class="btn btn-success w-100" disabled>
                    Show All Commit
                </button>
            </div>
        </div>

        <div class="row justify-content-center">
            <div class="col-12" id="commits-container">
                <div class="alert alert-secondary text-center">
                    Please select a User -> Repository -> Branch first
                </div>
            </div>
        </div>

    </div>
</div>
<?= $this->endSection() ?>
<?= $this->section('script') ?>
<script>
document.addEventListener('DOMContentLoaded', () => {
    // Ambil elemen
    const selectUser = document.getElementById('selectUser');
    const selectRepo = document.getElementById('selectRepo');
    const selectBranch = document.getElementById('selectBranch');
    const btnLoadCommits = document.getElementById('btnLoadCommits');
    const commitsContainer = document.getElementById('commits-container');

    // 1. Cari Repo otomatis saat User dipilih di Dropdown
    selectUser.addEventListener('change', async () => {
        const username = selectUser.value;
        
        // Reset pilihan di bawahnya jika user kembali memilih "-- Select User --"
        if (!username) {
            selectRepo.innerHTML = '<option value="">-- Select Repository --</option>';
            selectRepo.disabled = true;
            selectBranch.innerHTML = '<option value="">-- Select Branch --</option>';
            selectBranch.disabled = true;
            btnLoadCommits.disabled = true;
            return;
        }

        // Tampilkan loading
        selectRepo.innerHTML = '<option value="">Loading...</option>';
        selectRepo.disabled = true;
        selectBranch.innerHTML = '<option value="">-- Select Branch --</option>';
        selectBranch.disabled = true;
        btnLoadCommits.disabled = true;

        try {
            // PERBAIKAN: Menggunakan getrepos sesuai route kamu
            const url = `<?= base_url('api/gitea/getrepos') ?>/${username}`;
            const res = await fetch(url);
            
            if (!res.ok) {
                throw new Error(`HTTP error! status: ${res.status}`);
            }

            const response = await res.json();

            if (response.success && response.data.length > 0) {
                selectRepo.innerHTML = '<option value="">-- Select Repository --</option>';
                response.data.forEach(repo => {
                    selectRepo.innerHTML += `<option value="${repo.name}">${repo.name}</option>`;
                });
                selectRepo.disabled = false;
            } else {
                selectRepo.innerHTML = '<option value="">Repository not found</option>';
            }
        } catch (error) {
            console.error('Error fetching repos:', error);
            selectRepo.innerHTML = '<option value="">Error loading repository</option>';
            alert('Failed to fetch repository data from the server.');
        }
    });

    // 2. Ambil Branch ketika Repo dipilih
    selectRepo.addEventListener('change', async () => {
        const username = selectUser.value; 
        const repo = selectRepo.value;
        if (!repo) return;

        selectBranch.innerHTML = '<option value="">Loading...</option>';
        selectBranch.disabled = true;
        btnLoadCommits.disabled = true;

        try {
            // PERBAIKAN: Menggunakan getbranches sesuai route kamu
            const url = `<?= base_url('api/gitea/getbranches') ?>/${username}/${repo}`;
            const res = await fetch(url);
            const response = await res.json();

            if (response.success && response.data.length > 0) {
                selectBranch.innerHTML = '<option value="">-- Select Branch --</option>';
                response.data.forEach(branch => {
                    selectBranch.innerHTML += `<option value="${branch.name}">${branch.name}</option>`;
                });
                selectBranch.disabled = false;
            }
        } catch (error) {
            console.error('Error fetching branches:', error);
        }
    });

    // 3. Enable tombol Commit ketika branch dipilih
    selectBranch.addEventListener('change', () => {
        btnLoadCommits.disabled = !selectBranch.value;
    });

    // 4. Ambil Commits dan Render HTML
    btnLoadCommits.addEventListener('click', async () => {
        const username = selectUser.value;
        const repo = selectRepo.value;
        const branch = selectBranch.value;
        
        commitsContainer.innerHTML = '<div class="text-center"><div class="spinner-border text-primary" role="status"></div><p>Fetching commits...</p></div>';

        try {
            // set limit=50
            const limit = 50; 
            const url = `<?= base_url('api/gitea/getcommits') ?>/${username}/${repo}?sha=${encodeURIComponent(branch)}&limit=${limit}`;
            const res = await fetch(url);
            const response = await res.json();

            if (response.success) {
                renderCommits(response.data);
            } else {
                commitsContainer.innerHTML = `<div class="alert alert-danger">${response.message}</div>`;
            }
        } catch (error) {
            commitsContainer.innerHTML = `<div class="alert alert-danger">Failed to contact the server.</div>`;
        }
    });

    // Fungsi untuk me-render HTML persis seperti template PHP yang kamu buat
    function renderCommits(commits) {
        if (!commits || commits.length === 0) {
            commitsContainer.innerHTML = `
                <div class="alert alert-info text-center shadow-sm">
                    <i class="bi bi-info-circle me-2"></i> No commit history available for this branch.
                </div>`;
            return;
        }

        let html = '';
        commits.forEach((item, index) => {
            const message = item.commit?.message || 'No commit message';
            const authorName = item.author?.username || item.commit?.author?.name || 'Unknown';
            const avatar = item.author?.avatar_url || `https://ui-avatars.com/api/?name=${encodeURIComponent(authorName)}&background=random`;
            
            const dateObj = new Date(item.commit?.author?.date);
            const dateStr = dateObj.toLocaleDateString('id-ID', { day: '2-digit', month: 'short', year: 'numeric' }) + ', ' + 
                            dateObj.toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit' });

            const shaShort = item.sha ? item.sha.substring(0, 10) : '';
            const commitUrl = item.html_url || '#';
            
            const additions = item.stats?.additions || 0;
            const deletions = item.stats?.deletions || 0;
            const files = item.files || [];

            html += `
            <div class="card shadow-sm mb-3 border-0 commit-card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start gap-3 flex-sm-row flex-column">
                        
                        <div class="d-flex align-items-start flex-grow-1 overflow-hidden w-100">
                            <img src="${avatar}" class="rounded-circle avatar border me-3 flex-shrink-0" alt="Avatar ${authorName}" width="40" height="40">
                            
                            <div class="overflow-hidden w-100">
                                <a href="${commitUrl}" target="_blank" class="commit-message text-truncate d-block fw-bold text-decoration-none text-dark" title="${escapeHtml(message)}">
                                    ${escapeHtml(message)}
                                </a>
                                <div class="text-muted small mt-1 text-truncate">
                                    <i class="bi bi-person-fill"></i> <strong>${escapeHtml(authorName)}</strong> melakukan commit pada ${dateStr}
                                </div>
                            </div>
                        </div>
                        
                        <div class="text-sm-end flex-shrink-0 mt-3 mt-sm-0">
                            <a href="${commitUrl}" target="_blank" class="badge bg-light text-dark border font-monospace text-decoration-none fs-6 mb-2 d-inline-block">
                                <i class="bi bi-hash"></i>${shaShort}
                            </a>
                            <div class="stats small font-monospace fw-bold">
                                <span class="text-success me-2" title="Additions"><i class="bi bi-plus-square-fill"></i> ${additions}</span>
                                <span class="text-danger" title="Deletions"><i class="bi bi-dash-square-fill"></i> ${deletions}</span>
                            </div>
                        </div>
                    </div>`;

            if (files.length > 0) {
                html += `
                    <div class="mt-3 border-top pt-2 d-flex justify-content-between align-items-center">
                        <span class="text-muted small"><i class="bi bi-files"></i> Mengubah ${files.length} file</span>
                        <button class="btn btn-sm btn-outline-secondary rounded-pill" type="button" data-bs-toggle="collapse" data-bs-target="#files-${index}" aria-expanded="false">
                            Lihat Detail File <i class="bi bi-chevron-down"></i>
                        </button>
                    </div>
                    <div class="collapse mt-2" id="files-${index}">
                        <div class="card card-body bg-light border-0 p-3 file-list">
                            <ul class="list-unstyled mb-0 small font-monospace">`;
                
                files.forEach(file => {
                    let statusClass = 'text-secondary';
                    let icon = 'bi-file-earmark';
                    if (file.status === 'added') { statusClass = 'text-success'; icon = 'bi-file-earmark-plus-fill'; } 
                    else if (file.status === 'modified') { statusClass = 'text-warning'; icon = 'bi-file-earmark-diff-fill'; } 
                    else if (file.status === 'removed' || file.status === 'deleted') { statusClass = 'text-danger'; icon = 'bi-file-earmark-minus-fill'; }

                    html += `
                                <li class="mb-1 d-flex align-items-center">
                                    <i class="bi ${icon} ${statusClass} me-2 fs-6"></i>
                                    <span class="text-truncate" title="${escapeHtml(file.filename)}">
                                        ${escapeHtml(file.filename)}
                                    </span>
                                </li>`;
                });
                
                html += `   </ul>
                        </div>
                    </div>`;
            }

            html += `
                </div>
            </div>`;
        });

        commitsContainer.innerHTML = html;
    }

    function escapeHtml(unsafe) {
        if(!unsafe) return '';
        return unsafe
             .replace(/&/g, "&amp;")
             .replace(/</g, "&lt;")
             .replace(/>/g, "&gt;")
             .replace(/"/g, "&quot;")
             .replace(/'/g, "&#039;");
    }
});
</script>
<?= $this->endSection() ?>