-- TeleHealth Hub Botswana - Database Initialization Script
-- Create database
CREATE DATABASE IF NOT EXISTS telehealth_hub_botswana;
USE telehealth_hub_botswana;

-- Doctors/Healthcare Professionals table
CREATE TABLE IF NOT EXISTS healthcare_professionals (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    specialization VARCHAR(100),
    license_number VARCHAR(100) UNIQUE,
    clinic_name VARCHAR(200),
    clinic_address VARCHAR(255),
    availability_status ENUM('available', 'busy', 'offline') DEFAULT 'offline',
    consultation_fee DECIMAL(10, 2),
    bio TEXT,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Consultations table
CREATE TABLE IF NOT EXISTS consultations (
    id INT PRIMARY KEY AUTO_INCREMENT,
    patient_id INT NOT NULL,
    doctor_id INT NOT NULL,
    consultation_type ENUM('video', 'audio', 'chat') DEFAULT 'chat',
    status ENUM('scheduled', 'ongoing', 'completed', 'cancelled') DEFAULT 'scheduled',
    scheduled_date DATETIME,
    started_at DATETIME,
    ended_at DATETIME,
    symptoms TEXT,
    diagnosis TEXT,
    prescription TEXT,
    notes TEXT,
    cost DECIMAL(10, 2),
    payment_status ENUM('pending', 'completed', 'failed') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (doctor_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Appointments table
CREATE TABLE IF NOT EXISTS appointments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    patient_id INT NOT NULL,
    clinic_id INT,
    appointment_date DATETIME NOT NULL,
    status ENUM('scheduled', 'completed', 'cancelled', 'no-show') DEFAULT 'scheduled',
    reason TEXT,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (patient_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Clinics table
CREATE TABLE IF NOT EXISTS clinics (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(200) NOT NULL,
    address VARCHAR(255),
    phone VARCHAR(20),
    email VARCHAR(150),
    location_lat DECIMAL(10, 8),
    location_lng DECIMAL(11, 8),
    opening_hours VARCHAR(100),
    services TEXT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Health Library Content table
CREATE TABLE IF NOT EXISTS health_articles (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    category VARCHAR(100),
    content LONGTEXT,
    language ENUM('en', 'tn') DEFAULT 'en',
    author VARCHAR(100),
    is_published BOOLEAN DEFAULT TRUE,
    views INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- SMS Notifications table
CREATE TABLE IF NOT EXISTS sms_notifications (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    phone_number VARCHAR(20),
    message TEXT NOT NULL,
    notification_type ENUM('appointment_reminder', 'medication_alert', 'health_tip', 'emergency') DEFAULT 'health_tip',
    status ENUM('pending', 'sent', 'failed') DEFAULT 'pending',
    sent_at DATETIME,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Payments table
CREATE TABLE IF NOT EXISTS payments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    consultation_id INT,
    amount DECIMAL(10, 2),
    currency VARCHAR(3) DEFAULT 'BWP',
    payment_method ENUM('orange_money', 'card', 'bank_transfer') DEFAULT 'orange_money',
    transaction_id VARCHAR(100),
    status ENUM('pending', 'completed', 'failed', 'refunded') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (consultation_id) REFERENCES consultations(id) ON DELETE SET NULL
);

-- Symptom Checker History table
CREATE TABLE IF NOT EXISTS symptom_checks (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT,
    phone_number VARCHAR(20),
    symptoms TEXT,
    suggested_action TEXT,
    source ENUM('web', 'ussd') DEFAULT 'web',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);
CREATE INDEX idx_consultation_patient ON consultations(patient_id);
CREATE INDEX idx_consultation_doctor ON consultations(doctor_id);
CREATE INDEX idx_consultation_status ON consultations(status);
CREATE INDEX idx_appointment_patient ON appointments(patient_id);
CREATE INDEX idx_appointment_date ON appointments(appointment_date);
CREATE INDEX idx_sms_user ON sms_notifications(user_id);
CREATE INDEX idx_sms_status ON sms_notifications(status);
CREATE INDEX idx_payment_user ON payments(user_id);
CREATE INDEX idx_payment_status ON payments(status);

INSERT INTO clinics (name, address, phone, email, location_lat, location_lng, opening_hours, services) VALUES
('Gaborone Central Clinic', 'Plot 1234, Gaborone', '+267-3-123456', 'central@clinic.bw', -24.6282, 25.9241, '08:00-17:00', 'General Consultation, Maternal Health, Chronic Disease Management'),
('Francistown Medical Center', 'Main Street, Francistown', '+267-4-567890', 'francistown@clinic.bw', -21.1667, 27.5000, '08:00-17:00', 'Emergency Services, Surgery, Pediatrics');

INSERT INTO health_articles (title, category, content, language, author) VALUES
('HIV Prevention and Management', 'HIV/AIDS', 'Comprehensive guide on HIV prevention, testing, and treatment options available in Botswana...', 'en', 'Dr. Motswana'),
('Maternal Health: Pregnancy Care', 'Maternal Health', 'Essential information for pregnant women including prenatal care, nutrition, and delivery options...', 'en', 'Nurse Keabetswe'),
('Managing Tuberculosis', 'Infectious Diseases', 'Understanding TB symptoms, treatment regimens, and importance of medication adherence...', 'en', 'Dr. Motswana');
