<?php
// File: /admin/edit_artist_guide.php
session_start();
require_once __DIR__ . '/../db.php'; // optional, only if admin pages require DB

// security: only admin
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header('HTTP/1.1 403 Forbidden');
    echo "Access denied.";
    exit;
}

$content_dir = __DIR__ . '/../content';
$content_file = $content_dir . '/artist_guide_content.html';
$success = '';
$error = '';

// Ensure directory exists
if (!is_dir($content_dir)) {
    if (!mkdir($content_dir, 0755, true)) {
        $error = "Failed to create content directory: $content_dir";
    }
}

// Save posted content
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['guide_html'])) {
    $html = $_POST['guide_html'];

    // Basic check: ensure directory is writable
    if (!is_dir($content_dir) || !is_writable($content_dir)) {
        $error = "Content directory is not writable: $content_dir";
    } else {
        if (file_put_contents($content_file, $html) === false) {
            $error = "Failed to save content to $content_file";
        } else {
            $success = "Artist Guide saved successfully.";
        }
    }
}

// Load current content if exists
$existing = '';
if (file_exists($content_file)) {
    $existing = file_get_contents($content_file);
} else {
    // default starter content
    $existing = "<h2>United Cultural Forum - Artist User Guide</h2>\n<p>Use this editor to update the guide shown to artists.</p>";
}

// include admin header if you have one; adjust path if needed
// include __DIR__ . '/header.php';
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Edit Artist Guide - Admin</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="/css/style.css">
  <script src="https://cdn.tailwindcss.com"></script>
  <script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>
  <script>
    tinymce.init({
      selector: '#guide_html',
      height: 600,
      menubar: true,
      plugins: 'advlist autolink lists link image charmap paste help wordcount table',
      toolbar: 'undo redo | blocks | bold italic underline | alignleft aligncenter alignright | bullist numlist | link image | removeformat | help',
      content_css: '/css/style.css',
      forced_root_block: '',
      entity_encoding: 'raw'
    });
  </script>
</head>
<body class="bg-gray-50">
  <div class="max-w-6xl mx-auto p-6">
    <h1 class="text-2xl font-bold mb-4">Edit Artist Guide</h1>

    <?php if ($success): ?>
      <div class="mb-4 p-3 bg-green-50 border border-green-200 text-green-800"><?php echo htmlspecialchars($success); ?></div>
    <?php endif; ?>
    <?php if ($error): ?>
      <div class="mb-4 p-3 bg-red-50 border border-red-200 text-red-800"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <form method="post">
      <label for="guide_html" class="block text-sm font-medium text-gray-700 mb-2">Guide Content (HTML)</label>
      <!-- We output raw HTML into the textarea so TinyMCE loads it properly. This page is admin-only. -->
      <textarea id="guide_html" name="guide_html"><?php echo $existing; ?></textarea>

      <p class="text-sm text-gray-500 mt-2">Tip: paste from a rich editor or type directly. This content will be displayed as-is on the public page.</p>

      <div class="mt-4 flex space-x-2">
        <button type="submit" class="bg-ucf-green text-white px-4 py-2 rounded">Save</button>
        <a href="/artist_guide.php" target="_blank" class="bg-gray-200 px-4 py-2 rounded text-gray-700">View Public Page</a>
      </div>
    </form>

    <hr class="my-8">

    <div class="text-sm text-gray-600">
      <p>Content file: <code><?php echo htmlspecialchars($content_file); ?></code></p>
      <p>Make sure the <code>/content</code> directory is writable by the web server for saves to work.</p>
    </div>
  </div>
</body>
</html>
