<?php
session_start();
ob_start(); // Start output buffering
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "../include/config.php";
$userlogin=$_SESSION['email'];
$email=$_SESSION['email'];

$intake=$_SESSION['department'];
if (isset($_SESSION['email'])){?>
<?php

$textToCheck = '';
$result = null;
$error = '';


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['check'])) {
    
    $apiKey = '47pixMV6RZblGuTXtwZ0nyzMyBMl2E9HB1671kVyacc90fb4';
    $textToCheck = trim($_POST['text'] ?? '');
    
    if (empty($textToCheck)) {
        $error = 'Please enter some text to analyze.';
    } elseif (strlen($textToCheck) < 50) {
        $error = 'Please enter at least 50 characters for accurate analysis.';
    } else {
        $data = [
            'text' => $textToCheck,
            'version' => '3.0',
            'language' => 'en',
            'sentences' => true
        ];
        $ch = curl_init('https://api.gowinston.ai/v2/ai-content-detection');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json',
        ]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $response = curl_exec($ch);
        if ($response === false) {
            $error = 'cURL Error: ' . curl_error($ch);
        } else {
            $result = json_decode($response, true);
            
            if (json_last_error() !== JSON_ERROR_NONE) {
                $error = 'JSON Decode Error: ' . json_last_error_msg();
            } elseif (isset($result['error'])) {
                $error = 'API Error: ' . $result['error'];
            }
        }
        
        curl_close($ch);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://cdn.ckeditor.com/ckeditor5/41.0.0/classic/ckeditor.js"></script>

    <title>AI Content Detector</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
      
    
        .btn-check {
            background-color: #3498db;
            border: none;
            padding: 12px 25px;
            font-weight: 600;
            width: 100%;
            margin-top: 15px;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .btn-check:hover {
            background-color: #2980b9;
        }
        .result-box {
            margin-top: 30px;
            padding: 20px;
            border-radius: 8px;
            background-color: #f8f9fa;
            border-left: 4px solid #3498db;
        }
        .ai-score-high {
            color: #e74c3c;
            font-weight: bold;
        }
        .ai-score-medium {
            color: #f39c12;
            font-weight: bold;
        }
        .ai-score-low {
            color: #27ae60;
            font-weight: bold;
        }
        .sentence-score {
            font-size: 0.9rem;
            margin-left: 10px;
        }
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-family: inherit;
        }
        .form-text {
            color: #6c757d;
            font-size: 0.875rem;
            margin-top: 5px;
        }
        #editor{
            height:500px !important;
        }
    </style>
</head>
<body>
    <?php include "../include/nav.php";?>

  
      
<div class="content" style="">
    <br><br><br>
    <div class="container">
        <h1> <img src="logo.png" width="40"?> AI And Plagiarism Scanner</h1>
        
        <form method="post" action="">
            <div class="mb-3">
                <label for="textInput" class="form-label">Paste text to analyze:</label>
                <textarea id="editor" class="form-control" id="textInput" name="text" rows="50" placeholder="Paste your text here..." required><?php echo htmlspecialchars($textToCheck); ?></textarea>
                <div class="form-text">Enter at least 100 characters for accurate results.</div>
            </div>
            
            <button type="submit" name="check" class="btn-chec">
                Analyze Text
            </button>
        </form>
        
        <?php if (!empty($error)): ?>
            <div class="alert alert-danger mt-4"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        
        <?php if ($result && isset($result['score'])): ?>
            <div class="result-box mt-4">
                <h3>Analysis Results</h3>
                
                <?php
                $aiScore = $result['score'];
                $scoreClass = 'ai-score-high';
                $message = "High likelihood of AI-generated content.";
                
                if ($aiScore < 20) {
                    $scoreClass = 'ai-score-low';
                    $message = "Likely human-written content.";
                } elseif ($aiScore < 55) {
                    $scoreClass = 'ai-score-medium';
                    $message = "Mixed content - possibly AI-assisted.";
                }
                ?>
                
             
                
                <?php if (isset($result['sentences']) && is_array($result['sentences'])): ?>
                    <h5>Sentence Analysis:</h5>
                    <div class="sentence-analysis">
                        <?php foreach ($result['sentences'] as $sentence): ?>
                            <?php
                            $sentenceScore = $sentence['score'];
                            $sentenceClass = 'ai-score-high';
                            if ($sentenceScore < 33) {
                                $sentenceClass = 'ai-score-low';
                            } elseif ($sentenceScore < 66) {
                                $sentenceClass = 'ai-score-medium';
                            }
                            ?>
                            <div class="sentence-item mb-2">
                                <?php echo htmlspecialchars($sentence['text']); ?>
                                <span class="<?php echo $sentenceClass; ?> sentence-score">(AI: <?php echo $sentenceScore; ?>%)</span>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($result['credits_remaining'])): ?>
                    <div class="mt-3 pt-3 border-top">
                        <small class="text-muted">Credits remaining: <?php echo $result['credits_remaining']; ?></small>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
    <script>
ClassicEditor
    .create(document.querySelector('#editor'))
    .catch(error => {
        console.error(error);
    });
</script>
</body>
</html>
<?php

}else {

  header("location:../index.php");
}
?>