<?php
// data_owner/manage_my_datasources.php
session_start();
require_once '../config.php';
require_once '../includes/auth.php';
require_once '../classes/DataSource.php';

// Ensure only Data Owners can access this page
redirect_if_not_role('Data Owner');

$data_source_manager = new DataSource($pdo);
$user_id = $_SESSION['user_id'];
$owner_person_id = $_SESSION['person_id'];

$action = $_GET['action'] ?? 'list';
$ds_id = $_GET['id'] ?? null;
$datasource_data = [];

// Fetch dropdown data
$data_types = $data_source_manager->getAllDataTypes();
$categories = $data_source_manager->getAllCategories();
$primaryRulesMap = [];
foreach ($data_types as $type) {
    $typeName = $type['dspstds_name_en'] ?? null;
    $rules = $data_source_manager->getPrimaryFileRulesForType($typeName);
    $acceptList = [];
    foreach ($rules['extensions'] ?? [] as $ext) {
        $acceptList[] = '.' . strtolower($ext);
    }
    $primaryRulesMap[$type['pkdspstds_id']] = [
        'accept' => $acceptList,
        'description' => $rules['description'] ?? 'CSV, JSON, PDF, XLS, XLSX',
    ];
}
$defaultPrimaryRules = $data_source_manager->getPrimaryFileRulesForType(null);
$defaultPrimaryAccept = [];
foreach ($defaultPrimaryRules['extensions'] ?? [] as $ext) {
    $defaultPrimaryAccept[] = '.' . strtolower($ext);
}
$initialPrimaryDescription = $defaultPrimaryRules['description'] ?? 'CSV, JSON, PDF, XLS, XLSX';


// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title_en = trim($_POST['title_en'] ?? '');
    $title_kh = trim($_POST['title_kh'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $type_id = filter_var($_POST['type_id'] ?? '', FILTER_SANITIZE_NUMBER_INT);
    $category_id = filter_var($_POST['category_id'] ?? '', FILTER_SANITIZE_NUMBER_INT);
    $public_date = trim($_POST['public_date'] ?? '');
    $status = trim($_POST['status'] ?? 'Pending Review');
    $selectedDataType = null;
    if (!empty($type_id)) {
        $selectedDataType = $data_source_manager->getDataTypeById((int)$type_id);
    }
    $current_files = [
        'dspsds_filename' => trim($_POST['current_filename'] ?? ''),
        'dspsds_filename1' => trim($_POST['current_filename1'] ?? ''),
        'dspsds_filename2' => trim($_POST['current_filename2'] ?? ''),
        'dspsds_filename3' => trim($_POST['current_filename3'] ?? ''),
    ];

    $final_files = $current_files;
    $file_inputs = [
        'dspsds_filename' => 'data_file',
        'dspsds_filename1' => 'data_file1',
        'dspsds_filename2' => 'data_file2',
        'dspsds_filename3' => 'data_file3',
    ];
    $file_labels = [
        'dspsds_filename' => 'Primary Data File',
        'dspsds_filename1' => 'Questionnaire / Data Dictionary',
        'dspsds_filename2' => 'Protocol / User Guide',
        'dspsds_filename3' => 'Other Supporting Document',
    ];
    $remove_files = $_POST['remove_files'] ?? [];
    if (!is_array($remove_files)) {
        $remove_files = [$remove_files];
    }

    foreach ($file_inputs as $column => $inputName) {
        if (!isset($_FILES[$inputName]) || $_FILES[$inputName]['error'] === UPLOAD_ERR_NO_FILE) {
            continue;
        }

        try {
            if ($_FILES[$inputName]['error'] !== UPLOAD_ERR_OK) {
                throw new Exception('Upload error code: ' . $_FILES[$inputName]['error']);
            }
            $fileRules = null;
            if ($column === 'dspsds_filename') {
                $fileRules = $data_source_manager->getPrimaryFileRulesForType($selectedDataType['dspstds_name_en'] ?? null);
            }
            $uploadedName = $data_source_manager->handleDataSourceFileUpload($_FILES[$inputName], $fileRules);
            if ($uploadedName) {
                if (!empty($current_files[$column]) && $current_files[$column] !== $uploadedName) {
                    $oldPath = $data_source_manager->getUploadDir() . $current_files[$column];
                    if (is_file($oldPath)) {
                        unlink($oldPath);
                    }
                }
                $final_files[$column] = $uploadedName;
            }
        } catch (Exception $e) {
            $friendlyLabel = $file_labels[$column] ?? $inputName;
            set_message('File upload failed for ' . htmlspecialchars($friendlyLabel) . ': ' . $e->getMessage(), 'danger');
            $final_files[$column] = $current_files[$column];
        }
    }

    foreach ($remove_files as $column) {
        if (!array_key_exists($column, $final_files)) {
            continue;
        }
        if (!empty($current_files[$column])) {
            $oldPath = $data_source_manager->getUploadDir() . $current_files[$column];
            if (is_file($oldPath)) {
                unlink($oldPath);
            }
        }
        $final_files[$column] = '';
    }

    // Basic validation for required fields
    if (empty($title_en) || empty($type_id) || empty($category_id)) {
        set_message("Title, Data Type, and Category are required.", "danger");
        // Redirect to preserve form data or re-display form with errors
        // For now, we'll just redirect to list, but a better UX would be to stay on the form
        header("Location: manage_my_datasources.php?action=" . ($action === 'add_submit' ? 'add' : 'edit&id=' . $ds_id));
        exit();
    }

    // Determine the public date to pass to the add/update methods
    // The DataSource class's add/update methods have logic for this, so we'll pass it as a string or null
    $final_public_date = (!empty($public_date) && $status === 'Active') ? $public_date : null;


    if ($action === 'add_submit') {
        try {
            // Corrected call to addDataSource
            if ($data_source_manager->addDataSource(
                $type_id,
                $category_id,
                $owner_person_id, // Data owner is the logged-in person
                $final_files['dspsds_filename'],
                $title_en,
                $title_kh,
                $description,
                $status,
                $user_id, // User who registered it (logged-in user)
                $final_files['dspsds_filename1'],
                $final_files['dspsds_filename2'],
                $final_files['dspsds_filename3']
            )) {
                set_message("Data source added successfully!", "success");
            } else {
                set_message("Failed to add data source.", "danger");
            }
        } catch (Exception $e) {
            set_message("Error adding data source: " . $e->getMessage(), "danger");
        }
    } elseif ($action === 'edit_submit' && $ds_id) {
        try {
            // Corrected call to updateDataSource
            if ($data_source_manager->updateDataSource(
                $ds_id,
                $type_id,
                $category_id,
                $owner_person_id, // Data owner is the logged-in person
                $final_files['dspsds_filename'],
                $title_en,
                $title_kh,
                $description,
                $status,
                $user_id, // User who modified it (logged-in user)
                $final_files['dspsds_filename1'],
                $final_files['dspsds_filename2'],
                $final_files['dspsds_filename3']
            )) {
                set_message("Data source updated successfully!", "success");
            } else {
                set_message("Failed to update data source.", "danger");
            }
        } catch (Exception $e) {
            set_message("Error updating data source: " . $e->getMessage(), "danger");
        }
    }
    // Redirect after POST to prevent form resubmission
    header("Location: manage_my_datasources.php");
    exit();
}

// Handle GET actions
if ($action === 'edit' && $ds_id) {
    $datasource_data = $data_source_manager->getDataSourceById($ds_id);
    // Crucial security check: Ensure the logged-in owner actually owns this data source
    if (!$datasource_data || $datasource_data['fkisp_id_of'] != $owner_person_id) {
        set_message("Data source not found or you don't have permission to edit it.", "danger");
        header("Location: manage_my_datasources.php");
        exit();
    }
} elseif ($action === 'delete' && $ds_id) {
    $datasource = $data_source_manager->getDataSourceById($ds_id);
    // Crucial security check: Ensure the logged-in owner actually owns this data source
    if ($datasource && $datasource['fkisp_id_of'] == $owner_person_id) {
        // Delete associated file on the server
        $fileColumns = ['dspsds_filename', 'dspsds_filename1', 'dspsds_filename2', 'dspsds_filename3'];
        foreach ($fileColumns as $column) {
            if (!empty($datasource[$column])) {
                $filePath = $data_source_manager->getUploadDir() . $datasource[$column];
                if (is_file($filePath)) {
                    unlink($filePath);
                }
            }
        }
        if ($data_source_manager->deleteDataSource($ds_id)) {
            set_message("Data source deleted successfully!", "success");
        } else {
            set_message("Failed to delete data source.", "danger");
        }
    } else {
        set_message("Data source not found or you don't have permission to delete it.", "warning");
    }
    header("Location: manage_my_datasources.php");
    exit();
}

// Fetch data sources for the current owner for display
$my_data_sources = $data_source_manager->getDataSources($owner_person_id);
$uploadsWebPath = '../uploads/datasources/';

?>
<!DOCTYPE html>
<html lang="en">
<!-- Header -->
<?php
// Include header file for admin pages
include_once("../includes/header_owner.php");
?>
<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <?php
        // Include header file for admin pages
        include_once("../includes/nav_owner.php");
        ?>

        <!-- Page Content -->
        <div class="main-content">
            <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4 rounded-3">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#"> My Data Sources</a>
                    <div class="d-flex">
                        <span class="navbar-text me-3">
                            Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!
                        </span>
                    </div>
                </div>
            </nav>

            <div class="d-flex justify-content-between align-items-center mb-4">
                <a href="manage_my_datasources.php?action=add" class="btn btn-primary rounded">
                    <i class="fas fa-plus-circle me-2"></i> Add New Data Source
                </a>
            </div>

            <?php if (isset($_SESSION['message'])): ?>
                <div class="alert alert-<?= $_SESSION['message_type'] ?> alert-dismissible fade show rounded" role="alert">
                    <?= htmlspecialchars($_SESSION['message']) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                <?php
                unset($_SESSION['message']);
                unset($_SESSION['message_type']);
                ?>
            <?php endif; ?>

            <?php if ($action === 'add' || $action === 'edit'): ?>
                <div class="card shadow-sm rounded mb-4">
                    <div class="card-header bg-light rounded-top">
                        <h5 class="mb-0"><?= ($action === 'add' ? 'Add New' : 'Edit') ?> Data Source</h5>
                    </div>
                    <div class="card-body">
                        <form action="manage_my_datasources.php?action=<?= ($action === 'add' ? 'add_submit' : 'edit_submit&id=' . htmlspecialchars($ds_id)) ?>" method="POST" enctype="multipart/form-data">
                            <input type="hidden" name="current_filename" value="<?= htmlspecialchars($datasource_data['dspsds_filename'] ?? '') ?>">
                            <input type="hidden" name="current_filename1" value="<?= htmlspecialchars($datasource_data['dspsds_filename1'] ?? '') ?>">
                            <input type="hidden" name="current_filename2" value="<?= htmlspecialchars($datasource_data['dspsds_filename2'] ?? '') ?>">
                            <input type="hidden" name="current_filename3" value="<?= htmlspecialchars($datasource_data['dspsds_filename3'] ?? '') ?>">
                            <div class="mb-3">
                                <label for="dsTitleEn" class="form-label">Title (English)</label>
                                <input type="text" class="form-control rounded" id="dsTitleEn" name="title_en" value="<?= htmlspecialchars($datasource_data['dspsds_title_en'] ?? '') ?>" required>
                            </div>
                            <!--
                            <div class="mb-3">
                                <label for="dsTitleKh" class="form-label">Title (Khmer)</label>
                                <input type="text" class="form-control rounded" id="dsTitleKh" name="title_kh" value="<?//= htmlspecialchars($datasource_data['dspsds_title_kh'] ?? '') ?>">
                            </div>
                            -->
                            <div class="mb-3">
                                <label for="dsDescription" class="form-label">Description</label>
                                <textarea class="form-control rounded" id="dsDescription" name="description" rows="5"><?= htmlspecialchars($datasource_data['dspsds_description'] ?? '') ?></textarea>
                            </div>
                            <div class="mb-3">
                                <label for="dsType" class="form-label">Data Type</label>
                                <select class="form-select rounded" id="dsType" name="type_id" required>
                                    <option value="">Select Type...</option>
                                    <?php foreach ($data_types as $type): ?>
                                        <option value="<?= htmlspecialchars($type['pkdspstds_id']) ?>"
                                            <?= (isset($datasource_data['fkdspstds_id']) && $datasource_data['fkdspstds_id'] == $type['pkdspstds_id']) ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($type['dspstds_name_en']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label for="dsCategory" class="form-label">Category</label>
                                <select class="form-select rounded" id="dsCategory" name="category_id" required>
                                    <option value="">Select Category...</option>
                                    <?php foreach ($categories as $category): ?>
                                        <option value="<?= htmlspecialchars($category['pkdspscate_id']) ?>"
                                            <?= (isset($datasource_data['fkdspscate_id']) && $datasource_data['fkdspscate_id'] == $category['pkdspscate_id']) ? 'selected' : '' ?>>
                                            <?= htmlspecialchars($category['dspscate_title_en']) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>


                            <?php
$currentTypeForDescription = $datasource_data['fkdspstds_id'] ?? null;
if ($currentTypeForDescription && isset($primaryRulesMap[$currentTypeForDescription])) {
    $initialPrimaryDescription = $primaryRulesMap[$currentTypeForDescription]['description'] ?? $initialPrimaryDescription;
}
$fileInputsConfig = [
    [
        'label' => 'Primary Data File',
        'id' => 'dsDataFile',
        'name' => 'data_file',
        'column' => 'dspsds_filename',
        'help' => 'Upload a file that matches the selected data type.',
    ],
                                [
                                    'label' => 'Questionnaire / Data Dictionary',
                                    'id' => 'dsDataFile1',
                                    'name' => 'data_file1',
                                    'column' => 'dspsds_filename1',
                                    'help' => 'Upload a supporting document (PDF, XLSX, etc.)',
                                ],
                                [
                                    'label' => 'Protocol / User Guide',
                                    'id' => 'dsDataFile2',
                                    'name' => 'data_file2',
                                    'column' => 'dspsds_filename2',
                                    'help' => 'Upload a protocol or user guide (PDF, DOCX, etc.)',
                                ],
                                [
                                    'label' => 'Other Supporting Document',
                                    'id' => 'dsDataFile3',
                                    'name' => 'data_file3',
                                    'column' => 'dspsds_filename3',
                                    'help' => 'Optional additional document.',
                                ],
                            ];
                            ?>

                            <?php foreach ($fileInputsConfig as $config): ?>
                                <?php $existingFile = $datasource_data[$config['column']] ?? ''; ?>
                                <div class="mb-3">
                                    <label for="<?= htmlspecialchars($config['id']) ?>" class="form-label"><?= htmlspecialchars($config['label']) ?></label>
                                    <input type="file" class="form-control rounded" id="<?= htmlspecialchars($config['id']) ?>" name="<?= htmlspecialchars($config['name']) ?>">
                                    <?php if ($config['column'] === 'dspsds_filename'): ?>
                                        <small class="form-text text-muted">
                                            Allowed formats: <span id="primaryFileFormats"><?= htmlspecialchars($initialPrimaryDescription) ?></span>
                                        </small>
                                    <?php endif; ?>
                                    <?php if (!empty($existingFile)): ?>
                                        <?php
                                            $isUrl = preg_match('/^https?:\/\//i', $existingFile) === 1;
                                            $linkTarget = $isUrl ? $existingFile : $uploadsWebPath . rawurlencode($existingFile);
                                            $linkLabel = $isUrl ? 'View Link' : 'Download File';
                                        ?>
                                        <small class="form-text text-muted mt-2">
                                            Current file: <a href="<?= htmlspecialchars($linkTarget) ?>" target="_blank" rel="noopener"><?= htmlspecialchars($linkLabel) ?></a>
                                        </small>
                                        <div class="form-check mt-2">
                                            <input class="form-check-input" type="checkbox" id="remove_<?= htmlspecialchars($config['id']) ?>" name="remove_files[]" value="<?= htmlspecialchars($config['column']) ?>">
                                            <label class="form-check-label" for="remove_<?= htmlspecialchars($config['id']) ?>">Remove existing file</label>
                                        </div>
                                    <?php endif; ?>
                                    <?php if (!empty($config['help'])): ?>
                                        <small class="form-text text-muted"><?= htmlspecialchars($config['help']) ?></small>
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>

                            <div class="mb-3">
                                <label for="dsPublicDate" class="form-label">Public Date (Optional)</label>
                                <input type="date" class="form-control rounded" id="dsPublicDate" name="public_date" value="<?= htmlspecialchars($datasource_data['dspsds_public_date'] ?? '') ?>">
                            </div>
                            <div class="mb-3">
                                <label for="dsStatus" class="form-label">Status</label>
                                <select class="form-select rounded" id="dsStatus" name="status" required>
                                    <option value="Pending Review" <?= (isset($datasource_data['dspsds_status']) && $datasource_data['dspsds_status'] == 'Pending Review') ? 'selected' : '' ?>>Pending Review</option>
                                    <option value="Active" <?= (isset($datasource_data['dspsds_status']) && $datasource_data['dspsds_status'] == 'Active') ? 'selected' : '' ?>>Active</option>
                                    <option value="Inactive" <?= (isset($datasource_data['dspsds_status']) && $datasource_data['dspsds_status'] == 'Inactive') ? 'selected' : '' ?>>Inactive</option>
                                </select>
                            </div>
                            <div class="d-flex justify-content-end gap-2">
                                <a href="manage_my_datasources.php" class="btn btn-secondary rounded">Cancel</a>
                                <button type="submit" class="btn btn-primary rounded"><?= ($action === 'add' ? 'Add' : 'Update') ?> Data Source</button>
                            </div>
                        </form>
                    </div>
                </div>
            <?php endif; ?>

            <div class="card shadow-sm rounded">
                <div class="card-header bg-light rounded-top">
                    <h5 class="mb-0">My Data Sources</h5>
                </div>
                <div class="card-body">
                    <?php if (!empty($my_data_sources)): ?>
                        <div class="table-responsive">
                            <table class="table table-hover table-striped">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Title (EN)</th>
                                        <th>Type</th>
                                        <th>Category</th>
                                        <th>Status</th>
                                        <th>Data Source</th>
                                        <th>Questionnaire</th>
                                        <th>User Guide</th>
                                        <th>Supporting Doc</th>
                                        <th>Registered Date</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($my_data_sources as $ds): ?>
                                        <tr>
                                            <td><?= htmlspecialchars($ds['pkdspsds_id']) ?></td>
                                            <td><?= htmlspecialchars($ds['dspsds_title_en']) ?></td>
                                            <td><?= htmlspecialchars($ds['data_type_name']) ?></td> <!-- Corrected key -->
                                            <td><?= htmlspecialchars($ds['category_name']) ?></td> <!-- Corrected key -->
                                            <td>
                                                <span class="badge <?= ($ds['dspsds_status'] == 'Active' ? 'bg-success' : ($ds['dspsds_status'] == 'Pending Review' ? 'bg-warning' : 'bg-secondary')) ?>">
                                                    <?= htmlspecialchars($ds['dspsds_status']) ?>
                                                </span>
                                            </td>
                                            <?php
                                                $primaryFile = $ds['dspsds_filename'] ?? '';
                                                echo '<td>';
                                                if (!empty($primaryFile)) {
                                                    $isUrlPrimary = preg_match('/^https?:\/\//i', $primaryFile) === 1;
                                                    $primaryTarget = $isUrlPrimary ? $primaryFile : $uploadsWebPath . rawurlencode($primaryFile);
                                                    $primaryTitle = $isUrlPrimary ? 'External link' : 'Download Data Source';
                                                    echo '<a class="btn btn-sm btn-outline-primary rounded-pill" href="' . htmlspecialchars($primaryTarget) . '" target="_blank" rel="noopener" title="' . htmlspecialchars($primaryTitle) . '"><i class="fas fa-database"></i></a>';
                                                } else {
                                                    echo '<span class="text-muted">—</span>';
                                                }
                                                echo '</td>';
                                            ?>
                                            <?php
                                                $fileCells = [
                                                    'dspsds_filename1' => 'Questionnaire / Data Dictionary',
                                                    'dspsds_filename2' => 'Protocol / User Guide',
                                                    'dspsds_filename3' => 'Other Supporting Document',
                                                ];
                                                foreach ($fileCells as $column => $label) {
                                                    $fileName = $ds[$column] ?? '';
                                                    echo '<td>';
                                                    if (!empty($fileName)) {
                                                        $isUrl = preg_match('/^https?:\/\//i', $fileName) === 1;
                                                        $linkTarget = $isUrl ? $fileName : $uploadsWebPath . rawurlencode($fileName);
                                                        $titleAttr = $isUrl ? 'External link' : 'Download ' . $label;
                                                        echo '<a class="btn btn-sm btn-outline-primary rounded-pill" href="' . htmlspecialchars($linkTarget) . '" target="_blank" rel="noopener" title="' . htmlspecialchars($titleAttr) . '"><i class="fas fa-paperclip"></i></a>';
                                                    } else {
                                                        echo '<span class="text-muted">—</span>';
                                                    }
                                                    echo '</td>';
                                                }
                                            ?>
                                            <td><?= date('Y-m-d H:i', strtotime($ds['dspsds_reg_datetime'])) ?></td>
                                            <td>
                                                <a href="manage_my_datasources.php?action=edit&id=<?= htmlspecialchars($ds['pkdspsds_id']) ?>" class="btn btn-sm btn-info rounded me-1" title="Edit"><i class="fas fa-edit"></i></a>
                                                <a href="manage_my_datasources.php?action=delete&id=<?= htmlspecialchars($ds['pkdspsds_id']) ?>" class="btn btn-sm btn-danger rounded" title="Delete" onclick="return confirm('Are you sure you want to delete this data source? This will also delete related permissions and usage logs.');"><i class="fas fa-trash-alt"></i></a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info rounded mb-0">You have not added any data sources yet.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

<!-- Footer -->
<?php
// Include Footer file for owner pages
include_once("../includes/footer_owner.php");
?>   
<script>
document.addEventListener('DOMContentLoaded', function () {
    var typeSelect = document.getElementById('dsType');
    var fileInput = document.getElementById('dsDataFile');
    var formatsLabel = document.getElementById('primaryFileFormats');
    var primaryRules = <?php echo json_encode($primaryRulesMap, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); ?>;
    var defaultRule = <?php echo json_encode([
        'accept' => $defaultPrimaryAccept,
        'description' => $defaultPrimaryRules['description'] ?? 'CSV, JSON, PDF, XLS, XLSX',
    ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); ?>;

    function applyPrimaryFileRules() {
        if (!fileInput) {
            return;
        }
        var selected = typeSelect ? typeSelect.value : null;
        var rule = (selected && primaryRules[selected]) ? primaryRules[selected] : defaultRule;
        if (rule && Array.isArray(rule.accept) && rule.accept.length > 0) {
            fileInput.setAttribute('accept', rule.accept.join(','));
        } else {
            fileInput.removeAttribute('accept');
        }
        if (formatsLabel && rule && rule.description) {
            formatsLabel.textContent = rule.description;
        }
    }

    applyPrimaryFileRules();
    if (typeSelect) {
        typeSelect.addEventListener('change', applyPrimaryFileRules);
    }
});
</script>
</body>
</html>
