<?php
session_start();
//require_once 'includes/config.php';
require_once 'includes/database.php';
require_once 'Classes/Elearning.php';

$conn = new Database();
$elearning = new ELearning();

if (!isset($_SESSION['username'])) {
    echo '<div class="alert alert-danger">Unauthorized access</div>';
    exit();
}

if (isset($_POST['edit_material'])) {
    // Sanitize inputs
    $material_id = (int)$_POST['material_id'];
    $title = htmlspecialchars(trim($_POST['title']));
    $description = htmlspecialchars(trim($_POST['description']));
    $subject_id = (int)$_POST['subject'];
    $term_id = (int)$_POST['term'];
    $status = $_POST['status'];
    $duration = isset($_POST['duration']) ? trim($_POST['duration']) : null;
    $external_link = isset($_POST['external_link']) ? trim($_POST['external_link']) : null;

    // Validate required fields
    if (empty($title) || empty($subject_id) || empty($term_id)) {
        echo '<div class="alert alert-danger">
                <i class="fas fa-exclamation-triangle"></i> Please fill all required fields
              </div>';
        exit();
    }

    // Get current material
    $current_material = $elearning->getMaterialById($material_id);

    if (!$current_material) {
        echo '<div class="alert alert-danger">
                <i class="fas fa-exclamation-triangle"></i> Material not found
              </div>';
        exit();
    }

    // Check permission (only admin, superadmin or uploader can edit)
    $user_type = $elearning->checkUserType($_SESSION['username']);

    if (
        !in_array($user_type, ['admin', 'superadmin']) &&
        $current_material->uploaded_by != $_SESSION['username']
    ) {
        echo '<div class="alert alert-danger">
                <i class="fas fa-exclamation-triangle"></i> You do not have permission to edit this material
              </div>';
        exit();
    }

    // Validate external link if material type is link
    if ($current_material->material_type === 'link') {
        if (empty($external_link)) {
            echo '<div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle"></i> Please provide an external link
                  </div>';
            exit();
        }

        if (!filter_var($external_link, FILTER_VALIDATE_URL)) {
            echo '<div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle"></i> Please provide a valid URL
                  </div>';
            exit();
        }
    }

    // Prepare data for update
    $data = [
        'title' => $title,
        'description' => $description,
        'material_type' => $current_material->material_type,
        'external_link' => $external_link,
        'subject_id' => $subject_id,
        'term_id' => $term_id,
        'status' => $status,
        'duration' => $duration
    ];

    // Update material
    $result = $elearning->updateMaterial($material_id, $data);

    if ($result['success']) {
        echo '<div class="alert alert-success">
                <i class="fas fa-check-circle"></i> ' . $result['message'] . '
              </div>';
    } else {
        echo '<div class="alert alert-danger">
                <i class="fas fa-exclamation-triangle"></i> ' . $result['message'] . '
              </div>';
    }
} else {
    echo '<div class="alert alert-danger">Invalid request</div>';
}
