<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../../styles/stylesheet.css">
    <title>Admin Dashboard</title>
</head>
<body>
<div class="header">
    <nav>
        <h2>ELAD School of Potentials Therapy Center</h2>
        <ul>
            <li>Admin</li>
            <li><a href="#" id="logoutBtn">Logout</a></li>
        </ul> 
    </nav>
</div>

<div class="background-info">
    <h1>Admin Dashboard</h1>
    <h2>Welcome to the Admin Dashboard.</h2>
    <h2>Here you can manage users, view reports, and configure settings.</h2>
    <a href="dist/setappoint.html">Manage Users, Appointments and Transactions</a>
    <a href="../calendar/calendaredit.html">Manage Calendar and Schedules</a>
    <a href="analytics.html">Website Analytics</a>
</div>

<div class="footer">
    <div class="social-links">
        <a href="https://www.facebook.com/ELADSchoolofPotentials" class="footer-style-button">Facebook</a>
    </div>
    <p>&copy; 2024 ELAD School of Potentials Therapy Center</p>
</div>

<script type="module">
    import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.2/firebase-app.js";
    import { getAuth, signOut, onAuthStateChanged, setPersistence, browserLocalPersistence } from "https://www.gstatic.com/firebasejs/9.22.2/firebase-auth.js"; 

    const firebaseConfig = {
        apiKey: "AIzaSyCZfi1aIc3zDdZD7NR7kN2Jm4hnCtPHWcQ",
        authDomain: "appointment-scheduling-s-57d01.firebaseapp.com",
        databaseURL: "https://appointment-scheduling-s-57d01-default-rtdb.asia-southeast1.firebasedatabase.app",
        projectId: "appointment-scheduling-s-57d01",
        storageBucket: "appointment-scheduling-s-57d01.firebasestorage.app",
        messagingSenderId: "1069885306589",
        appId: "1:1069885306589:web:b31768194add4087754d34",
        measurementId: "G-KQ4PQZZZBM"
    };

    const app = initializeApp(firebaseConfig);
    const auth = getAuth(app);

    // Use browserLocalPersistence for more persistent logins
    setPersistence(auth, browserLocalPersistence)
        .then(() => {
            console.log('Persistence set to local.');
        })
        .catch((error) => {
            console.error('Error setting persistence:', error);
        });

    document.addEventListener('DOMContentLoaded', (event) => {
        onAuthStateChanged(auth, (user) => {
            console.log('Auth state changed triggered.');
            setTimeout(() => {
                if (user) {
                    console.log("User logged in:", user.email);
                    localStorage.setItem('userEmail', user.email);
                    if (user.email === 'admin.control@mail.com') {
                        console.log("Admin is logged in:", user.email);
                        if (window.location.pathname !== '/path/to/admin/dashboard') {
                            window.location.href = '/path/to/admin/dashboard';
                        }
                    } else {
                        console.log("User is logged in:", user.email);
                        if (window.location.pathname !== '/path/to/user/homepage') {
                            window.location.href = '/path/to/user/homepage';
                        }
                    }
                } else {
                    console.log("User is not logged in.");
                    localStorage.removeItem('userEmail');
                    window.location.href = '../auth/login.html';
                }
            }, 10800000); // timeout ms
        });
    });

    const signOutUser = () => {
        signOut(auth)
            .then(() => {
                console.log('Signed out successfully');
                localStorage.removeItem('userEmail');
                window.location.href = '../auth/login.html'; 
            })
            .catch((error) => {
                console.log('Error signing out:', error);
                alert("Error: " + error.message);
            });
    };

    const logoutBtn = document.getElementById('logoutBtn');
    if (logoutBtn) {
        logoutBtn.addEventListener('click', signOutUser);
    } else {
        console.error("Logout button not found!");
    }
</script>
</body>
</html>
