/* ===== 서비스 워커 해제 (push-sw.js 제외) ===== */
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.getRegistrations().then(function(regs) { regs.forEach(function(r) {
        if (!r.active || !r.active.scriptURL.includes('push-sw.js')) { r.unregister(); }
    }); });
    if (typeof caches !== 'undefined') { caches.keys().then(function(ns) { ns.forEach(function(n) { caches.delete(n); }); }); }
}

/* ===== Config ===== */
const API_BASE = '/api';

/* ===== Token ===== */
function _cookieGet(n) { const m = document.cookie.match(new RegExp('(?:^|; )' + n + '=([^;]*)')); return m ? decodeURIComponent(m[1]) : null; }
function _cookieRemove(n) { let c = n + '=; path=/; max-age=0'; if (location.hostname.includes('technicalkorea.net')) c += '; domain=.technicalkorea.net; secure; samesite=lax'; document.cookie = c; }
function getToken() { return _cookieGet('sso_token') || localStorage.getItem('sso_token'); }
function getLoginUrl() {
    const h = location.hostname;
    const t = Date.now(); // 캐시 버스팅
    if (h.includes('technicalkorea.net')) return location.protocol + '//tkds.technicalkorea.net/dashboard?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
    return location.protocol + '//' + h + ':30780/dashboard?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
}
function decodeToken(t) { try { const b = atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')); return JSON.parse(new TextDecoder().decode(Uint8Array.from(b, c => c.charCodeAt(0)))); } catch { return null; } }

/* ===== 리다이렉트 루프 방지 ===== */
const _REDIRECT_KEY = '_sso_redirect_ts';
function _safeRedirect() {
    const last = parseInt(sessionStorage.getItem(_REDIRECT_KEY) || '0', 10);
    if (Date.now() - last < 5000) { console.warn('[tkuser] 리다이렉트 루프 감지'); return; }
    sessionStorage.setItem(_REDIRECT_KEY, String(Date.now()));
    location.href = getLoginUrl();
}

/* ===== API ===== */
async function api(path, opts = {}) {
    const token = getToken();
    const res = await fetch(API_BASE + path, { ...opts, headers: { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : '', ...(opts.headers||{}) } });
    if (res.status === 401) { throw new Error('인증 만료'); }
    const data = await res.json();
    if (!res.ok) throw new Error(data.error || data.detail || '요청 실패');
    return data;
}

/* ===== Toast ===== */
function showToast(msg, type = 'success') {
    document.querySelector('.toast-message')?.remove();
    const el = document.createElement('div');
    el.className = `toast-message fixed bottom-4 right-4 px-4 py-3 rounded-lg text-white z-[10000] shadow-lg ${type==='success'?'bg-emerald-500':'bg-red-500'}`;
    el.innerHTML = `<i class="fas ${type==='success'?'fa-check-circle':'fa-exclamation-circle'} mr-2"></i>${escapeHtml(msg)}`;
    document.body.appendChild(el);
    setTimeout(() => { el.classList.add('opacity-0'); setTimeout(() => el.remove(), 300); }, 3000);
}

/* ===== Escape ===== */
function escapeHtml(str) { if (!str) return ''; const d = document.createElement('div'); d.textContent = str; return d.innerHTML; }

/* ===== Helpers ===== */
const DEPT_FALLBACK = { production:'생산', quality:'품질', purchasing:'구매', design:'설계', sales:'영업' };
let departmentsCache = [];
async function loadDepartmentsCache() {
    try {
        const r = await api('/departments');
        departmentsCache = (r.data || r).filter(d => d.is_active !== 0 && d.is_active !== false);
    } catch(e) { console.warn('부서 캐시 로드 실패:', e); }
}
function deptLabel(d, deptId) {
    if (deptId && departmentsCache.length) {
        const dept = departmentsCache.find(x => x.department_id === deptId);
        if (dept) return dept.department_name;
    }
    return DEPT_FALLBACK[d] || d || '';
}
function formatDate(d) { if (!d) return ''; return d.substring(0, 10); }
function getSeoulToday() { return new Date().toLocaleDateString('en-CA', { timeZone: 'Asia/Seoul' }); }
function escHtml(s) { if (!s) return ''; const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }

/* ===== Logout ===== */
function doLogout() {
    if (!confirm('로그아웃?')) return;
    _cookieRemove('sso_token'); _cookieRemove('sso_user'); _cookieRemove('sso_refresh_token');
    ['sso_token','sso_user','sso_refresh_token','token','user','access_token','currentUser','current_user','userInfo','userPageAccess'].forEach(k => localStorage.removeItem(k));
    location.href = getLoginUrl() + '&logout=1';
}

/* ===== State ===== */
let currentUser = null;
// null = admin(제한없음), Set = 허용된 탭만
let currentUserAllowedTabs = null;

/* ===== Init ===== */
async function init() {
    // 쿠키 우선 검증: 쿠키 없고 localStorage에만 토큰이 있으면 정리
    const cookieToken = _cookieGet('sso_token');
    const localToken = localStorage.getItem('sso_token');
    if (!cookieToken && localToken) {
        ['sso_token','sso_user','sso_refresh_token','token','user','access_token',
         'currentUser','current_user','userInfo','userPageAccess'].forEach(k => localStorage.removeItem(k));
        _safeRedirect();
        return;
    }

    const token = getToken();
    if (!token) { _safeRedirect(); return; }
    const decoded = decodeToken(token);
    if (!decoded) { _safeRedirect(); return; }
    sessionStorage.removeItem(_REDIRECT_KEY);

    // 쿠키에서 읽었으면 localStorage에도 백업 (다음 방문 시 쿠키 소실 대비)
    if (!localStorage.getItem('sso_token')) localStorage.setItem('sso_token', token);

    currentUser = { id: decoded.user_id||decoded.id, username: decoded.username||decoded.sub, name: decoded.name||decoded.full_name, role: (decoded.role||decoded.access_level||'').toLowerCase(), partner_company_id: decoded.partner_company_id || null };

    // 협력업체 계정 차단
    if (currentUser.partner_company_id) {
        location.href = location.hostname.includes('technicalkorea.net')
            ? 'https://tkpurchase.technicalkorea.net/partner-portal.html'
            : location.protocol + '//' + location.hostname + ':30480/partner-portal.html';
        return;
    }

    const dn = currentUser.name || currentUser.username;
    document.getElementById('headerUserName').textContent = dn;
    document.getElementById('headerUserRole').textContent = currentUser.role === 'admin' ? '관리자' : '사용자';
    document.getElementById('headerUserAvatar').textContent = dn.charAt(0).toUpperCase();

    try {
        if (currentUser.role === 'admin' || currentUser.role === 'system') {
            document.getElementById('tabNav').classList.remove('hidden');
            document.getElementById('adminSection').classList.remove('hidden');
            await loadDepartmentsCache();
            await loadUsers();
        } else {
            // tkuser 탭별 권한 확인
            try {
                const result = await api(`/permissions/users/${currentUser.id}/effective-permissions`);
                if (result.permissions) {
                    // TKUSER_PAGES에서 TAB_PERM_MAP 동적 생성
                    const TAB_PERM_MAP = {};
                    if (typeof TKUSER_PAGES !== 'undefined') {
                        Object.values(TKUSER_PAGES).flat().forEach(p => { TAB_PERM_MAP[p.key] = p.tab; });
                    }

                    currentUserAllowedTabs = new Set();
                    for (const [permKey, tabName] of Object.entries(TAB_PERM_MAP)) {
                        const info = result.permissions[permKey];
                        if (info && info.can_access) {
                            currentUserAllowedTabs.add(tabName);
                        }
                    }

                    // 허용된 탭이 있으면 tabNav 표시
                    if (currentUserAllowedTabs.size > 0) {
                        document.getElementById('tabNav').classList.remove('hidden');
                        // 비허용 탭 버튼 숨김
                        document.querySelectorAll('.tab-btn').forEach(btn => {
                            const tabName = btn.getAttribute('data-tab');
                            if (tabName) {
                                // permissions 탭은 admin 전용
                                if (tabName === 'permissions') {
                                    btn.style.display = 'none';
                                } else if (!currentUserAllowedTabs.has(tabName)) {
                                    btn.style.display = 'none';
                                }
                            }
                        });

                        // tkuser.users 권한 시 adminSection + passwordChangeSection 표시
                        if (currentUserAllowedTabs.has('users')) {
                            document.getElementById('adminSection').classList.remove('hidden');
                            await loadDepartmentsCache();
                            await loadUsers();
                        } else {
                            // users 권한 없음 → tab-users 숨기고 첫 허용 탭으로 자동 전환
                            document.getElementById('tab-users').classList.add('hidden');
                            switchTab([...currentUserAllowedTabs][0]);
                        }
                    } else {
                        // 아무 권한 없음 → 비밀번호 변경만 표시 (fallback)
                        document.getElementById('passwordChangeSection').classList.remove('hidden');
                    }
                }
            } catch(e) {
                console.error('[tkuser] 권한 확인 실패:', e);
                showToast('권한 정보를 불러올 수 없습니다', 'error');
            }
        }
    } catch (e) {
        console.error('[tkuser] init 오류:', e);
    }
    // 알림 벨 로드
    _loadNotificationBell();

    setTimeout(() => {
        document.querySelectorAll('.fade-in').forEach(el => el.classList.add('visible'));
        // URL ?tab= 파라미터로 탭 자동 전환 (화이트리스트 + URL 정리)
        const ALLOWED_TABS = ['users','projects','workplaces','workers','departments',
            'permissions','issueTypes','tasks','vacations','vacationSettings','partners','vendors',
            'consumables','notificationRecipients','equipments'];
        const urlTab = new URLSearchParams(location.search).get('tab');
        if (urlTab && ALLOWED_TABS.includes(urlTab)) {
            const tabBtn = document.querySelector(`.tab-btn[data-tab="${urlTab}"]`);
            if (tabBtn && tabBtn.style.display !== 'none') {
                tabBtn.click();
                const url = new URL(location);
                url.searchParams.delete('tab');
                history.replaceState(null, '', url);
            }
        }
    }, 50);
}

/* ===== 알림 벨 ===== */
function _loadNotificationBell() {
    const s = document.createElement('script');
    s.src = (location.hostname.includes('technicalkorea.net') ? 'https://tkds.technicalkorea.net' : location.protocol + '//' + location.hostname + ':30000') + '/shared/notification-bell.js?v=4';
    document.head.appendChild(s);
}

/* ===== Modal ESC 닫기 ===== */
document.addEventListener('keydown', e => {
    if (e.key === 'Escape') {
        document.querySelectorAll('.modal-overlay:not(.hidden)')
            .forEach(m => m.classList.add('hidden'));
    }
});
