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

header('Content-Type: application/json');

if(!isset($_SESSION['username']) || !isset($_POST['conflict_id'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
    exit();
}

$conn = new Database();
$conflictId = $_POST['conflict_id'];
$notes = isset($_POST['resolution_notes']) ? trim($_POST['resolution_notes']) : '';
$resolvedBy = $_SESSION['username'];

try {
    $sql = "UPDATE timetable_conflicts SET
            is_resolved = 1,
            resolved_by = :resolved_by,
            resolved_at = NOW()
            WHERE id = :id";

    $conn->query($sql);
    $conn->bind(':resolved_by', $resolvedBy);
    $conn->bind(':id', $conflictId);

    if($conn->execute()) {
        echo json_encode([
            'success' => true,
            'message' => 'Conflict marked as resolved'
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Failed to update conflict'
        ]);
    }
} catch(Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error: ' . $e->getMessage()
    ]);
}