<?php
session_start();
include __DIR__ . '/auth.php'; // sets $CAN_EDIT

include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$appFile = __DIR__ . '/../data/applications.txt';
$uploadDir = 'uploads/applications/';

$applications = [];

/* ------------------------------------------------------------
   LOAD APPLICATIONS
------------------------------------------------------------ */
if (file_exists($appFile)) {
    $lines = file($appFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {
        $parts = explode('|', $line);

        if (count($parts) >= 12) {
            $applications[] = [
                'id'          => $parts[0],
                'name'        => $parts[1],
                'email'       => $parts[2],
                'location'    => $parts[3],
                'about'       => $parts[4],
                'music'       => $parts[5],
                'role'        => $parts[6],
                'experience'  => $parts[7],
                'availability'=> $parts[8],
                'why'         => $parts[9],
                'audio'       => $parts[10],
                'timestamp'   => $parts[11]
            ];
        }
    }
}

/* ------------------------------------------------------------
   DELETE APPLICATION (EDIT PERMISSION REQUIRED)
------------------------------------------------------------ */
if (isset($_GET['delete'])) {

    if (!$CAN_EDIT) {
        die("<h1>Access Denied</h1><p>You cannot delete applications.</p>");
    }

    $deleteId = $_GET['delete'];
    $newList = [];

    foreach ($applications as $app) {
        if ($app['id'] !== $deleteId) {
            $newList[] = implode('|', [
                $app['id'],
                $app['name'],
                $app['email'],
                $app['location'],
                $app['about'],
                $app['music'],
                $app['role'],
                $app['experience'],
                $app['availability'],
                $app['why'],
                $app['audio'],
                $app['timestamp']
            ]);
        }
    }

    file_put_contents($appFile, implode("\n", $newList));
    header("Location: applications.php");
    exit;
}
?>

<style>
.application-card {
    padding: 30px;
    margin-bottom: 30px;
    text-align: left;
}

.application-card h2 {
    color: #4ff;
    margin-bottom: 10px;
    text-shadow: 0 0 10px rgba(0,255,255,0.45);
}

.application-field {
    margin-bottom: 12px;
    line-height: 1.6;
}

.application-label {
    font-weight: bold;
    color: #4ff;
    text-shadow: 0 0 6px rgba(0,255,255,0.45);
}

.audio-link {
    display: inline-block;
    margin-top: 10px;
    padding: 8px 14px;
    background: rgba(0,0,0,0.6);
    border: 2px solid #4ff;
    border-radius: 8px;
    color: #4ff;
    text-decoration: none;
    box-shadow: 0 0 6px rgba(0,255,255,0.45);
}

.audio-link:hover {
    background: rgba(0,0,0,0.9);
    transform: scale(1.05);
}
</style>

<div class="main-wrap">
    <div class="content-panel">

        <h1 class="section-title center-text">📝 Applications</h1>

        <div class="vinyl-divider center-divider"></div>

        <?php if (!empty($applications)): ?>

            <?php foreach ($applications as $app): ?>
                <div class="neon-card application-card">

                    <h2><?= htmlspecialchars($app['name']) ?></h2>

                    <div class="application-field">
                        <span class="application-label">Email:</span>
                        <?= htmlspecialchars($app['email']) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Location:</span>
                        <?= htmlspecialchars($app['location']) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">About:</span><br>
                        <?= nl2br(htmlspecialchars($app['about'])) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Music:</span><br>
                        <?= nl2br(htmlspecialchars($app['music'])) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Role Applied For:</span>
                        <?= htmlspecialchars($app['role']) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Experience:</span>
                        <?= htmlspecialchars($app['experience']) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Availability:</span>
                        <?= htmlspecialchars($app['availability']) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Why Join:</span><br>
                        <?= nl2br(htmlspecialchars($app['why'])) ?>
                    </div>

                    <div class="application-field">
                        <span class="application-label">Submitted:</span>
                        <?= htmlspecialchars($app['timestamp']) ?>
                    </div>

                    <?php if (!empty($app['audio'])): ?>
                        <a href="<?= $uploadDir . htmlspecialchars($app['audio']) ?>" class="audio-link" target="_blank">
                            🎧 Listen to Demo
                        </a>
                    <?php endif; ?>

                    <br><br>

                    <?php if ($CAN_EDIT): ?>
                        <a href="applications.php?delete=<?= $app['id'] ?>"
                           class="button"
                           style="background:#900;color:#fff;">
                            Delete Application
                        </a>
                    <?php endif; ?>

                </div>

                <div class="vinyl-divider center-divider"></div>

            <?php endforeach; ?>

        <?php else: ?>

            <div class="content-box center-text">
                <p>No applications yet.</p>
            </div>

        <?php endif; ?>

    </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
