<?php
/**
 * ميزات متقدمة: ولاء، زيارات منزلية، حزم فحوصات، عينات، تنبيهات
 */
$config = require __DIR__ . '/../config/config.php';
$pdo = new PDO(
    'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'] . ';charset=utf8mb4',
    $config['db']['user'],
    $config['db']['pass'],
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$changes = [];

// نقاط الولاء للمرضى
try {
    $pdo->exec("ALTER TABLE patients ADD COLUMN loyalty_points DECIMAL(10,2) NOT NULL DEFAULT 0");
    $changes[] = "patients.loyalty_points";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column') === false) $changes[] = "Error: " . $e->getMessage();
}

// حركات نقاط الولاء
$pdo->exec("CREATE TABLE IF NOT EXISTS loyalty_transactions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    patient_id INT NOT NULL,
    order_id INT NULL,
    points DECIMAL(10,2) NOT NULL,
    type ENUM('earn','redeem') NOT NULL,
    note VARCHAR(255) NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES patients(id)
)");
$changes[] = "loyalty_transactions";

// طلبات الزيارات المنزلية
$pdo->exec("CREATE TABLE IF NOT EXISTS home_visit_requests (
    id INT AUTO_INCREMENT PRIMARY KEY,
    patient_id INT NULL,
    patient_name VARCHAR(150) NOT NULL,
    patient_phone VARCHAR(30) NOT NULL,
    address TEXT NOT NULL,
    visit_date DATE NOT NULL,
    visit_time_start VARCHAR(10) NULL,
    visit_time_end VARCHAR(10) NULL,
    status ENUM('pending','confirmed','completed','cancelled') NOT NULL DEFAULT 'pending',
    order_id INT NULL,
    tenant_id INT NULL,
    notes TEXT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES patients(id)
)");
$changes[] = "home_visit_requests";

// حزم الفحوصات
$pdo->exec("CREATE TABLE IF NOT EXISTS test_packages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(150) NOT NULL,
    description TEXT NULL,
    price DECIMAL(10,2) NOT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    tenant_id INT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
$changes[] = "test_packages";

$pdo->exec("CREATE TABLE IF NOT EXISTS test_package_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    package_id INT NOT NULL,
    test_id INT NOT NULL,
    FOREIGN KEY (package_id) REFERENCES test_packages(id) ON DELETE CASCADE,
    FOREIGN KEY (test_id) REFERENCES tests(id)
)");
$changes[] = "test_package_items";

// العينات: باركود وموقع
try {
    $pdo->exec("ALTER TABLE samples ADD COLUMN barcode VARCHAR(100) NULL");
    $changes[] = "samples.barcode";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column') === false) $changes[] = "Error samples.barcode: " . $e->getMessage();
}
try {
    $pdo->exec("ALTER TABLE samples ADD COLUMN location VARCHAR(100) NULL");
    $changes[] = "samples.location";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column') === false) $changes[] = "Error samples.location: " . $e->getMessage();
}
try {
    $pdo->exec("ALTER TABLE samples ADD COLUMN disposal_date DATETIME NULL");
    $changes[] = "samples.disposal_date";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column') === false) $changes[] = "Error samples.disposal_date: " . $e->getMessage();
}

// مخزون: تاريخ الصلاحية
try {
    $pdo->exec("ALTER TABLE inventory_items ADD COLUMN expiry_date DATE NULL");
    $changes[] = "inventory_items.expiry_date";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column') === false) $changes[] = "Error expiry: " . $e->getMessage();
}

// روابط مشاركة التقارير
$pdo->exec("CREATE TABLE IF NOT EXISTS report_share_links (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT NOT NULL,
    token VARCHAR(64) NOT NULL UNIQUE,
    expires_at DATETIME NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (order_id) REFERENCES lab_orders(id)
)");
$changes[] = "report_share_links";

foreach ($changes as $c) echo $c . "\n";
echo "Done.\n";
